With Facebook Canvas and Page Apps, it’s sometimes useful to know if a user has Liked a particular page. It could be an entry requirement for a competition / prize draw or simply something you want to record / check. To check Likes using this method (or any other method that uses the API), you need you ask for the user_likes
permission.
There is a simple FQL query you can use to check if a user likes a specific page – you need to know the user_id
and page_id
beforehand, however. The FQL query is:
SELECT page_id FROM page_fan WHERE uid=me() AND page_id= [page_id]
You can replace me()
with a specific user_id
, otherwise me()
will default to the current logged-in user. An example of this query in action is:
SELECT page_id FROM page_fan WHERE uid=me() AND page_id=26406392257
Which returns the following output from the API:
{ "data": [ { "page_id": 26406392257 } ] }
If the user has Liked the page, the page_id
will be returned. Otherwise, you will get a empty result. In the above example, the user has liked the particular page, so the page_id
is returned.
The Graph API call would look something like:
https://graph.facebook.com/fql?q=SELECT page_id FROM page_fan WHERE uid=me() AND page_id=[page_id]&access_token=[access_token]
An access_token
is required to make the above call – a page access_token
or app access_token
will not work.
Checking Like Status Using Graph API and Facebook PHP SDK 4.0.x
If you don’t want to use FQL to retrieve the Like status, or you are using Graph API v2.0+, you can get the Like status as follows:
You can make a Graph API call to the /me/likes
endpoint, but if you append the $page_id
to the end of this, you can get the Like status of a particular page.
Hi Niraj.
I’ve an app in sandbox mode, I’m trying to get if I’m fan of a page but it returns me empty, but I’m fan of tested page… what could be happen?
thanks in advance
Is the page you created published? It could be one of the reasons why it’s not showing correctly in the FQL query. It could also be associated with restrictions on your page (e.g. country or age restrictions).
For using this, I have to include some facebook sdk files or not?
Yes, you need to use this code with one of the Facebook SDKs. It will work with the JavaScript, PHP or other SDKs that support FQL or Graph API calls.
thanks! I didn’t saw it! 🙂
This does not seem to work unless you use me(). I tested this on my own page and could not get any results. Am I wrong in saying this?
You also need to make sure you ask for the user_likes permission, otherwise you’ll just get empty data back. You will have to use me() as you are looking at the currently logged in user. If you have long-lived access_tokens, you can use a User ID to query Likes for another user.
I’ve got “error”: {
“message”: “(#12) fql is deprecated for versions v2.1 and higher”,
“type”: “OAuthException”,
“code”: 12
}
how can I do it with php sdk v.4?
thx
I’ve updated the article to give you an example of how to do this using the new Facebook PHP SDK.