Facebook API: Check If A User Is Fan Of A Facebook Page
There are two three methods to check if a user is fan of a certain page:
Using the Graph API /USER_ID/likes/PAGE_ID method
Facebook has just released another Graph API way for you to check for this information:
https://graph.facebook.com/me/likes/PAGE_ID
&access_token=ACCESS_TOKEN
This would return either an empty data array:
Array
(
[data] => Array
(
)
)Or Something like:
Array
(
[data] => Array
(
[0] => Array
(
[name] => Real Madrid C.F.
[category] => Professional sports team
[id] => 19034719952
[created_time] => 2011-05-03T20:53:26+0000
)
)
)So this is how we check using the PHP-SDK:
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/PAGE_ID");
if( !empty($likes['data']) )
echo "I like!";
else
echo "not a fan!";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
// rest of code here
?>Similar request in Javascript:
FB.api('/me/likes/PAGE_ID',function(response) {
if( response.data ) {
if( !isEmpty(response.data) )
alert('You are a fan!');
else
alert('Not a fan!');
} else {
alert('ERROR!');
}
});
// function to check for an empty object
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}More about the isEmpty function here.
Using the REST API pages.isFan method
While Facebook is in the process of deprecating the REST API, pages.isFan is still working and can be called from the new PHP-SDK:
$isFan = $facebook->api(array( "method" => "pages.isFan", "page_id" => $page_id, "uid" => $user_id )); if($isFan === TRUE) echo "I'm a fan!";
Here are two notes to consider:
- Using
if($isFan)is not enough because:If the user’s pages are set to less than everyone privacy, you must ask the user for the
user_likesextended permission and include a valid user access token in the API call.So basically you may get an ugly “Permissions error” message and it’ll actually validate!
- If the user you are checking against has a valid session then you don’t need the
uidparameter
Same API call but in Javascript:
FB.api({ method: 'pages.isFan', page_id: '184484190795', uid: 'user_id' }, function(resp) {
if (resp == true) {
Log.info('user_id likes the Application.');
} else if(resp.error_code) {
Log.error(resp.error_msg);
} else {
Log.error("user_id doesn't like the Application.");
}
});
Once again, you need to check the response of the call if true or not…using if(resp) is NOT enough!
The example in the Facebook JavaScript Test Console is a bit misleading (the does-like example), since if you don’t grant the user_likes permission and just try it with uid: '579187141' it would result that the user likes the application which is not true!
P.S: I just decremented my user id to get the above user which (it seems) he has a strict privacy for pages he likes!
Back to topUsing the FQL page_fan table
We can query the page_fan table to check if the user is a fan of a page or not like so:
$result = $facebook->api(array( "method" => "fql.query", "query" => "SELECT uid FROM page_fan WHERE uid=$user_id AND page_id=$page_id" )); if(count($result)) echo "$user_id is a fan!";
Following the same concept as the previous method, if you have a user valid session you can use me() instead of $user_id.
The FQL in Javascript:
FB.api({
method: 'fql.query',
query: 'SELECT uid FROM page_fan WHERE uid=user_id AND page_id=page_id'
}, function(resp) {
if (resp.length) {
alert('A fan!')
} else {
alert('Not a fan!');
}
}
);
Back to topNotes
- Always ask for the
user_likespermission before calling any of the above to make sure you get a correct result - Remember when having a valid session, you can use the
pages.isFanwithout theuidparameter and you can useme()instead of the user id when using FQL. - For FQL, you can always use
https://api.facebook.com/method/fql.query?query=QUERYas mentioned in the documentation
- http://www.toptankntr.com Toptan Parça Kontor
- Hadisneslanovic
- Vinicio
- Vinicio
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/profile.php?id=536656275 Ahmad Jamaludin
- http://www.facebook.com/AlfaTrion Farhan Jiwani
- Flpldev
- http://www.facebook.com/people/Awais-Qarni/1794017457 Awais Qarni
- Hadisneslanovic
- http://www.elitescort.net Bayan Eskort
- Martin Bächtold
- Hadisneslanovic
- jack
- Shoaib
- KYCO
- xaluan
- Lily
- Kyle
- kiko
- http://www.facebook.com/naved.naddy Naved Chogle
- http://www.facebook.com/ivory.santos.3 Ivory Santos
- Mgc
- http://www.facebook.com/javithkhanj Javith Khan
- http://www.facebook.com/sudhir600 Sudhir K Gupta
Advertisment
Recent Tutorials
- How To: Create A User Photo Albums Browser Using Facebook Graph API
- How To: Upload A Photo To A User’s Profile Using Facebook Graph API
- How To: Check Status And RSVP To Facebook Events Using Graph API & FQL
- Facebook Javascript SDK Best Practices
- How To: Create Facebook Events Using Graph API – Advanced






