Site icon Web Niraj

Facebook API: Checking if a User Likes a Specific Page

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:

See the gist on github.

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.

Exit mobile version