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.

Back to top
 

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_likes extended 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 uid parameter

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 top
 

Using 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 top
 

Notes

  • Always ask for the user_likes permission 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.isFan without the uid parameter and you can use me() instead of the user id when using FQL.
  • For FQL, you can always use https://api.facebook.com/method/fql.query?query=QUERY as mentioned in the documentation
Back to top
  • http://www.toptankntr.com Toptan Parça Kontor

    great good…this post deserves nothing …hahaha just joking :P …nice post

    • Hadisneslanovic

      hey im getting error with this php scipt:

      PHP Fatal error:  Uncaught OAuthException: An active access token must be used to query information about the current user.

      • Vinicio

        Hi, that active token is only passed by facebook. If your are browsing your canvas page alone, this error will be shown. Try running it through a fanpage

  • Vinicio

    i found this tutorial verry useful, but what if i want to check is current user is fan or not of a page, without the autorization process? is that possible?

  • http://www.facebook.com/profile.php?id=536656275 Ahmad Jamaludin

    ive been looking for this
    simple and direct!
    you ROCK o/

  • http://www.facebook.com/AlfaTrion Farhan Jiwani

    Thank you this made my whole experience pretty speedy!

  • Flpldev

    Hi Ibrahim, I tried your code but $user returns empty. Is the code still up to date?

  • http://www.facebook.com/people/Awais-Qarni/1794017457 Awais Qarni

    Ibrahim you are a rocker dude. Very Well explained articl

  • Hadisneslanovic

    can you help me, im using this php code to check if user liked my page and i gettings this error:

    PHP Fatal error:  Uncaught OAuthException: An active access token must be used to query information about the current user.

  • http://www.elitescort.net Bayan Eskort

    REST API pages.isFan method very good. thanks for information..

  • Martin Bächtold

    Do I have to ask the user for the “user_likes” permission in order to use /USER_ID/likes/PAGE_ID? It’s not mentioned on https://developers.facebook.com/blog/post/540/  (section “Graph API available for checking if a user likes a specific page”).

    What do you think about this Graph API call: /PAGE_ID/members/USER_ID?
    I’ve not found any documentation about it but it seems to work for me.

    • Hadisneslanovic

      yes

  • jack

    is it works also with a wordpress page ??? I would like to show a pop up windows in fancybox that cointains the like box of my facebook page for only user that are not fan of my page.

  • Shoaib

    Hi,
        i need help…i added the username  and page id as well.. but it not showing any thing i.e user is fane or not ….:(((

  • KYCO

    I have been using a very similar Javascript approach to getting the Like status, but as of today (4/25/2012), it no longer works!  Using your sample code above, I get the ERROR! alert.  Seems that response.data is null today.  Worked fine as of 4/24/2012 5:15pm – no longer worked as of 4/25/2012 8:00am…….weird. 
    Anyone have any suggestions?  
    Suffering same issues?  

  • xaluan

    great .. could you post the tut how to determine is the user authorized the specific app , if not show the login button request for that app !

  • Lily

    Where can I get a page_id?

    • Kyle

      1) Go to your page and access the Admin Panel. 2) Click “Edit PageUpdate Info” 3) Look at the URL in your browser’s location bar 4) Find the id=XXXXXXXX in the querystring, where XXXXXXXX is the page id.
      Happy coding :-)

  • kiko

    Is there anyway to see who likes your page?
    I want to do in http://cuponzeria.com

  • http://www.facebook.com/naved.naddy Naved Chogle

    Thanks.. It helped a lot.. works well…

  • http://www.facebook.com/ivory.santos.3 Ivory Santos

    is this still working, particularly the PHP-SDK method? thanks in advance Ibrahim Faour

  • Mgc

    thanks very very thankssssss :)

  • http://www.facebook.com/javithkhanj Javith Khan

    Hello , A warm to welcome to all .. I am very tired to get all fan page likes user id .. its just display whose friends of mine … please help me to find how to get all page likes user list ??

  • http://www.facebook.com/sudhir600 Sudhir K Gupta

    hey can u provide ../src/facebook.php

  • Elias Ayala

    the first example I get blank

  • Goran Gorčo Atanasovski

    All the examples I get, that I don’t like the page, whose ID I put. any advice on this ?

  • Vikas

    Thanks for the nice post, can you tell me is it possible to get if the logged in FB user had liked a particular page or not (its ok if it returns false in case the page likes are private). I don’t want to ask for user_likes permissions though.

  • Mera Rizal

    I will give you 10000 Targeted Facebook user IDS, Emails From Any Fanpage, Group,Event
    http://www.fiverr.com/darshana2013/give-you-10000-targeted-facebook-user-ids-emails-from-any-fanpage-groupevent

  • http://www.elbwb.com.br everaldo

    How to delete like of one page facebook with api? help me