Facebook SDK checking if user is already fan - facebook

Hello I would like to check if user is already fan of my fanpage. I already found ready script to do that but it have so much parsing errors. Could you help me and fix that code?
<!--?php require('facebook.php');
$config = array(
'appId' =?--> 'your facebook app id',
'secret' => 'your facebook app secret code',
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if (isset($user_id)) {
try {
$likes = $facebook->api('/me/likes/your_facebook_page_id_here', 'GET');
if (!empty($likes['data'])) // if user has liked the page then $likes['data'] wont be empty otherwise it will be empty
{
echo 'Thank you for liking our fan page!';
// you can write some custom code here to award users some points or some badge
}
else {
echo 'You have not liked our fan page! Like it now:';
?>
<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Farkhitech&width&height=290&colorscheme=light&show_faces=true&header=true&stream=false&show_border=true&appId=1392604484339363" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:290px;" allowtransparency="true"></iframe> //replace this with your own Facebook like box code
<!--?php }
} catch (FacebookApiException $e) {
$login_url = $facebook-?-->getLoginUrl();
echo 'Please click here to login into your Facebook account.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please lick here to login into your Facebook account';
}
?>

You would need to authorize the user with the user_likes permissions and then use the following API call:
/me/likes/[page-id]
As you can see, that´s the API call in your example code. Without telling us EXACTLY what errors you get, it´s impossible to help. But it does not matter anyway, because:
Incentivizing likes is not allowed, you can´t reward users in any way for liking your Page according to the platform policy.
You would need to get user_likes approved by Facebook, else it will only work for users with a role in the App. Facebook will definitely not approve it for something that is not allowed.
In other words: Not possible and not allowed.

Related

Publishing comments on a user post wtih graph api

I am new to facebook graph api
i have written a small application for posting comments on the user profile.
the following is the code:
<?php
require_once 'config.php';
/* Get a valid session */
$user= $facebook->getUser();
$me = null;
if($user) {
//Check if session is valid
$me = $facebook->api('/me');
}
if($me) {
echo 'User is logged in and has a valid session';
print_r($me);
$id=getfirstpostid($facebook->api('/me/posts'));
echo "id is ".$id;
$message="This is done programatically";
$comment_id = $facebook->api('/'.$id.'/comments','POST',
array('message' => $message,));
//$like_id = $facebook->api('/'.$id.'/likes', 'POST');
}
else {
//$login_url = $facebook->getLoginUrl();
//header("Location: {$login_url}");
echo 'Session expired or user has not logged in yet.
Redirecting...';
$login_url=$facebook->getLoginUrl(array('req_perms'=>'publish_actions,user_likes,offline_access,manage_pages,user_friends',));
echo '<script>top.location.href="'. $login_url .'";</script>';
}
function getfirstpostid($feeds)
{
return $feeds['data'][0]['id'];
}
?>
<?php
$logoutUrl = $facebook->getLogoutUrl(array('next' => 'http://distanceeducationhelpline.org/',));
?>
<a href='<?php echo $appBaseUrl?>about.php' target='_top'>
About Us</a>
<a href='http://www.9itech.com' target='_top'>External Link</a>
<br/>
<br/>
<a href ="#" onclick="top.location.href='<?php echo $logoutUrl; ?>';
return false;">Logout</a>
a. While creating the app ,i have not provided secure Canvas Url and accordingly the Canvas page does not show up in facebook.
is it due to the secure canvas url not being proper that blank page comes in the canvas page url.
b. when i am accessing the user profile the profile does not ask for permission to post on his profile even though i have given permissions .
i am not able to get the likes and post comments to work.kindly update if there is any problem in code.
do we need to get access tokens in this case or the code is proper.
thanks

When the user agree and give my Facebook app permisions?

I develop a Facebook application and I use PHP-sdk I use the algorithm described in the official documentation to allow user joining my app then I store his/her user_id in the database. My app uses publish_stream permissions so it publishes posts to the user's time line automatically.
I found that the application, always, does not able to publish posts for some of the registered users in the database (not all of them). I account this for they may be registered to the database before they give approval for the required permissions. The following is a blue print for the code that I use from the official documentation of Facebook api:
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
/* $user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];*/
//Here I run the code to save.
saveTheUserIdToRecordInTheDB($user_id);
//hypothetical function to save
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
My, main, question is: If the user clicked on the login link, and then he/she does not continue for any reason (losing the internet connection, refusing giving the permissions to the app, etc), would it possible the hypothetical function saveTheUserIdToRecordInTheDB() to run and save that user's Facebook's ID to the database?
Or What's the explanation for there are some users are not posts publishable?

CodeIgniter and Facebook Connect

I am trying to integrate my Codeigniter website with the Facebook PHP SDK. What I want to do is let a user share an article from my site on their facebook wall, if they are logged into a facebook account. My library appears to load correctly, but everytime I try to do something, I get some kind of error... primarily with the auth. getUser does not appear to return the correct results. I set up my facebook application and set the config vars for my library, but no luck. It says I am not logged into facebook. When I click on the "login" anchor, the link takes me to the same page, but with the facebook url, and doesn't ask me to login with the app. Here's my code:
function facebook($article_id){
$config = array(
'appId' => '276870792431073',
'secret' => '8d49eee575413fb9a8063d22f65dbf6a'
);
$this->load->library('facebook', $config);
$user = $this->facebook->getUser();
if($user){
try {
$user_profile = $this->facebook->api('/me');
} catch (FacebookApiException $e){
error_log($e);
$user = null;
}
}
if($user){
$article = $this->article->fetch_article($article_id);
$config = array(
'message' => 'I just read an '.anchor('articles/'.url_title($article['title']).'/'.$article_id, 'article').' on '.anchor('', 'TrackTheOutbreak.com').'!',
);
$this->facebook->api('/me/feed', 'post', $config);
} else {
$data['MESSAGE_TITLE'] = 'Authentication Error';
$data['MESSAGE_TEXT'] = 'You must be logged into an existing Facebook account to use this feature. Click '.anchor($this->facebook->getLoginUrl(), 'here').' to login.';
$this->parser->parse('error_body.tpl', $data);
}
}
In order to access a users information and post anything to their wall you first need to get a access token from them. To do that you need to make sure that you have gained their permission through Facebook's FB_login (and then Open Graph). I would double check with this guide and make sure that you have everything set up properly to post to their timeline.
https://developers.facebook.com/docs/reference/javascript/FB.login/
I hope this helps

Facebook status update code fails to update wall

I got the sample code from Facebook to post a message. At first it worked fine. The next day I tried to clean up the code and it stopped working. I brought back the sample code from Facebook, to take out all my changes, and it still did not work.
I put in print statements to follow the code.
$user_id is false, so it displayes a login in. The login in no longer goes to a login in screen. It calls my return url, which then sees $user_id as being false and diplays the login in again.
I did notice when my call back was being called, I had the following values:
v?state=452c9492a3d51962909f5e000bcb0965
and code
code=AQDucDgsdc3lZFVN2MC3Oj2oB0n1LT4FjOrG3MbgwL4uhh--LS-mRdtjU-6oSUZxsR6UhTKuUDVn3hrasJAe5r1I6ksIDJz1nnTo1mjCqEInJKvQ2qK5A-N3_Nt5buGLqsirb8ccg21N4nWVWkk_iRePwX9f68qK1j-2O6E_USpKvRJeNP3bcwLBiTBJpDVu7aA#=
I was wondering maybe when I was trying to clean up the code and kept running it, Facebook flagged my app as spamming? I posted 26 posts to my wall - is that too many? And how would I check if my app has been flagged as a spammy app?
This is the sample code I used
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('facebook.php');
$config = array(
'appId' => 'myappid',
'secret' => 'mysecret',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
print ("tryinmg to do a poast");
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
print ("face book exception log in");
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
echo '<br />logout';
} else {
print ("no user login in do login");
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
Naturally, Facebook has a limit for the number of post per day your app can make to a user wall. And FB have policy for user to report spammy apps as well.
The exact maximum number of posts depends on your application FB ranking. In my experience, 26 posts on a user wall is near the limit for new application.
If that is the case, you should receive the error message from Facebook, which says something like "you have reached limit of posts..."
P/s: It seems you need time to refactoring your question, though. If I have not met this situation before, I won't understand your question.

facebook : load the wall content of a facebook page in a website

I have a website and a facebook page for the website. For most of the news update, I post them on the facebook page's wall.
Now I want to show the wall's content of that facebook page on my website, just like the facebook's Like Box with "stream" enabled: http://developers.facebook.com/docs/reference/plugins/like-box/
However, I only want to show the stream, and perfectly can show it in my own presentation. So is it possible to get only the content of a facebook page by facebook's API?
I think if you use the SDK you can get it by using
fb->api("/{id}/feed");
There's a php and a javascript sdk.
But you also need an access token now to get the feed of a page.
EDIT: Here's a copy of example.php (from the php-sdk) modified for your purpose.
include_once("facebook-sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID GOES HERE',
'secret' => 'SECRET GOES HERE',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// This is where we grab the posts
$wall_posts = $facebook->api('/courseyou/posts');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>Login with Facebook</div>
<?php endif ?>
<h3>Wall Posts</h3>
<?php
foreach ($wall_posts["data"] as $post) {
echo "<p>".$post["from"]["name"].": ".$post["message"]."</p>";
}
?>
As far as I know you need an access token to view a page's posts/feed with the api, which makes me think that you need the user to login with facebook.... I'm fairly new to this, but you should look into how to get an access token, because you need one for this.