Snipe.Net Geeky, sweary things.

Getting Like Count on a Facebook Fan Page Using the PHP SDK & FQL


I’m working on a project that will end up being age-gated because of graphic content. (No, not porn, unfortunately, but a blood and gore video game is almost as good.) It’s also like-gated, so we needed to fetch the number of people who liked the page, so that certain content would be unlocked when the like-count reached a certain number.

On a non-age-gated page, this is pretty simple using the Open Graph. Take the open graph page for vitaminwater, for example:

"id": "50540568485",
"name": "vitaminwater",
"picture": "",
"link": "",
"category": "Company",
"likes": 2234069,
"website": "[removed for brevity]",
"username": "vitaminwater",
"mission": "[removed for brevity]",
"products": "[removed for brevity]"

As you can see, the number of likes (as well as some other information) is right there in the JSON response.

On an age-gated page, however, accessing the open graph gives simply:

For example:

Because the page is age-gated, that open graph data isn’t available without an authentication token (so that Facebook knows who you are and that you’re old enough to view that content). Of course, since this is just going to be a tab, we didn’t want to force users to have to allow or authenticate with the app, so that sucked.

We contemplated running some cron jobs to fetch and cache the number of likes using fake users to authenticate against. And then we realized that we’re idiots.

Using the simple Open Graph call to determine the number of likes and shares on any object, we were able to get the number of likes without having to force anyone to authenticate.

"": {
"id": "",
"shares": 465574

Unfortunately, as Jangla pointed out in the comments, that actually returns a hybrid number that Facebook uses that consists of likes, shares and comments. It’s fine for a general number most of the time, but if you’re building something that is dependent on the actual number of likes in order to unlock something, a “sort of close” number just won’t cut it.

Enter a simple FQL query. The page ID for Snipe.Net’s Facebook page is 116633947708:

require_once 'facebook-php-sdk/src/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'YOUR APP ID',
  'secret' => 'YOUR SECRET KEY',
  'cookie' => true,

$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 116633947708;'
echo $fb_fans = '<li>'.$result[0]['fan_count'].' likes for';

The page ID for Battlefield (which is age-gated, remember) is 308775617671:

$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 308775617671;'
echo $fb_fans = '</li><li>'.$result[0]['fan_count'].' likes for battlefield';

If you’d like to automatically add the commas in the numbers (like 1,000,000), just throw number_format() around $result[0]['fan_count'].

Hope that helps!

About the author


I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead or read more...

  • I love you.

  • Jangla

    There’s an issue with your method. The ‘shares’ value in the Open Graph solution you suggest are not actually ‘likes’. It’s a combined count of likes, comments and user shares of the original page post. In the same way that the ‘likes’ count on a Like Button social plugin are actually a count of likes, comments and shares of that comment from the resulting FB feed story.

    Several of us have been badgering Facebook for sometime to correct this but they seem unwilling to put the work in.

    • @ec2c00f724572392dbcd6407d4cbc934:disqus

       – you’re right, of course – it’s the age-gating that causes the issue, since it’s unauthenticated, and therefore Facebook doesn’t know how old you are. For our purposes, this was sufficient – sorry I forgot to mention the combo number in the article.

      My preference would actually be to let the standard open graph info be available regardless of age-gating so that a standard open graph call can be made for very basic details:

      “id”: “50540568485”,
      “name”: “vitaminwater”,
      “picture”: “foo”,
      “link”: “”,
      “likes”: 2523961,
      “category”: “Food/beverages”,
      “website”: “foo”,
      “username”: “vitaminwater”,
      “mission”: “hydration for every occasion – morning, noon, night.”,
      “products”: “foo.”

      That said, you can fetch the actual likes using FQL:

      // Create our Application instance.
      $facebook = new Facebook(array(
        ‘appId’ => ‘YOUR_APP_ID’,
        ‘secret’ => ‘XXXXXXXXXXXXXXXXXXX’,
        ‘cookie’ => true,

      echo $fb_fans = $result[0][‘fan_count’];
      $result = $facebook->api(array(
      ‘method’ => ‘fql.query’,
      ‘query’ => ‘select fan_count from page where page_id = 308775617671;’
      echo $fb_fans = $result[0][‘fan_count’].’ likes’;

      I’ll update this article accordingly.

    • The article has been update d- thanks again.

  • Adrian Nieto

    Hello, i’m having problems getting the data for age-gated pages. It doesnt matter if I’m old enough or not. Even so.. with an Access Token i’m getting the info i want. I tried your code and the second query its returning nothing. Have you find a way to sovle this problem? Thanks in advance.

    • The code works fine – I have it live in several places, but it does seem to be acting strange today.

      Treating the FB url as an external url still works fine tho.

      $external_result = $facebook->api(array(
      ‘method’ => ‘fql.query’,
      ‘query’ => ‘SELECT share_count, like_count, comment_count, total_count, click_count FROM link_stat WHERE url=””;’
      echo ”.number_format($external_result[0][‘like_count’]).’ likes battlefield (https)’;

      echo ‘

      echo '


    • Meh. Disqus may have eaten my code there. It’s here:

  • Andrew

    I’ve written a script which will do everything for you.

    • Uhh, this *is* a script that does everything for you…

  • Facebook fans for your fan page is making it the absolute best and most engaging that it can be. There are many easy things that you can do that will help make your Facebook fan page more popular. It’s hard to stand out amongst all the different Facebook pages competing for the average person’s attention, so you have to make sure you don’t skip some essential strategies if you don’t want to get lost in the noise.

  • Julien Liabeuf

    Thanks a lot! Very useful for my projects!

  • See this link -> its a newer version where you can get the facebook likes, comment, and share counts using facebook FQL and get it via json and display it as a text. you can manipulate the view and design it in a way you want. Its so handy, simpler Code, and soo useful

  • jigar suthar


By snipe
Snipe.Net Geeky, sweary things.

About Me

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead or read more...

Get in Touch