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:
https://graph.facebook.com/vitaminwater

{
"id": "50540568485",
"name": "vitaminwater",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/187945_50540568485_6973057_s.jpg",
"link": "https://www.facebook.com/vitaminwater",
"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:
false

For example: https://graph.facebook.com/battlefield

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.

https://graph.facebook.com/?ids=http://www.facebook.com/battlefield

{
"http://www.facebook.com/battlefield": {
"id": "http://www.facebook.com/battlefield",
"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 snipe.net';

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

$result = $facebook-&gt;api(array(
'method' =&gt; 'fql.query',
'query' =&gt; '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!

Advertisement

468x60_makemoney

Advertisement

Themeforest
image
Previous post

App tabs disappearing in HTTPS-Only mode

plate
Next post

iPhone/iPad Apps for Coders and Sysadmins

snipe

snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I'm the CTO at Mass Mosaic and the CEO of Grokability, Inc. in San Diego, CA. 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:

      https://graph.facebook.com/vitaminwater

      {
      “id”: “50540568485”,
      “name”: “vitaminwater”,
      “picture”: “foo”,
      “link”: “https://www.facebook.com/vitaminwater”,
      “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=”https://www.facebook.com/battlefield”;’
      ));
      echo ”.number_format($external_result[0][‘like_count’]).’ likes battlefield (https)’;

      echo ‘

      ';
      print_r($external_result);
      echo '

      ‘;

    • Meh. Disqus may have eaten my code there. It’s here: http://pastebin.com/vAqHjA1v

  • Andrew

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

    http://www.andy-howard.com/numberOfFacebookLikes/index.html

    • 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 -> http://www.upgradedtutorials.info/php/getting-facebook-likes-shares-and-comment-counts-using-php-function/ 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

    ANY HELP ME I HAVE ALL DETAIL GET FROM FACEBOOK WITH FACEBOOK FAN PAGES BUT ONLY I AM NOT GETTING PERTICULAR PAGE ALL POST,EVENTS,LIKE AND etc. PLEASE HELP ME ANY PERSON SUGGESTION ME