Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Checking if a User Likes a Specific Page

Facebook API: Checking if a User Likes a Specific Page

9

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.

Facebook, Facebook Graph API, FQL

9 comments on “Facebook API: Checking if a User Likes a Specific Page”

  1. jorge says:
    April 26, 2013 at 11:39 PM

    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

    Reply
  2. Niraj Shah says:
    April 30, 2013 at 10:06 AM

    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).

    Reply
  3. ubaid says:
    June 4, 2013 at 3:18 PM

    For using this, I have to include some facebook sdk files or not?

    Reply
    • Niraj Shah says:
      June 4, 2013 at 3:26 PM

      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.

      Reply
  4. jorge says:
    June 4, 2013 at 5:14 PM

    thanks! I didn’t saw it! 🙂

    Reply
  5. Anton Swanevelder says:
    October 15, 2013 at 1:53 PM

    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?

    Reply
    • Niraj Shah says:
      November 9, 2013 at 11:31 AM

      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.

      Reply
  6. Василий Леонов says:
    August 16, 2014 at 5:28 PM

    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

    Reply
    • Niraj Shah says:
      August 18, 2014 at 10:23 AM

      I’ve updated the article to give you an example of how to do this using the new Facebook PHP SDK.

      Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

PSN Profile

Tags

Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache API Application Backup Bug Command Line Cordova Example Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Google Nexus One Hacking HTML5 Image Manipulation iOS iPhone JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2022 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy