These days, every business wants more and more ‘Likes’ for their Facebook pages if their Facebook application supports its own custom tab. And this is kinda becoming the basic requirement for every such Facebook app these days so here’s how you can achieve that using Graph API PHP SDK. This helps us choosing the content which we show to the user and if he has not liked the page yet, we can ask the user to do that first.
require 'facebook.php';
$app_id = "your app id";
$app_secret = "your app secret";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
if ($like_status == 1){
echo 'User likes this page';
}
else{
echo 'User doesn\'t like this page';
}
You can check all the other data by printing the $signed_request array like this:
echo '
'; print_r($signed_request); echo '
‘;
Among other info, you can also retrieve the page ID of the current Facebook page using the above information from array. $signed_request[‘page’][‘id’] can help you in showing tab specific content.
Hope that helps.
Cheers!
Hi! Does this work on websites to or it works only on page tab?
It works everywhere. It’s php sdk Graph API example. You can load your iFrame in websites or page tabs.
Hello, where is facebook.php that the code is requiring at the very beginning?
How should the code look like if I want to hide the following code ONLY to the users that do like the fanpage?
thecode dsagdsa dsa gsa
?
You can download Facebook php files from here https://github.com/facebook/php-sdk
Hi,
I just noticed an error in your code, it should be:
echo ‘No, User doesn\’t like this page’;
or
echo “No, User doesn’t like this page”;
on the second last line line.
But otherwise an awesome tutorial.
Thanks ( :
fixed now, Yes, I missed the single quote. Thanks. 🙂