Snipe.Net Geeky, sweary things.

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

G

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:

[php]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 = ‘

  • ‘.$result[0][‘fan_count’].’ likes for snipe.net’;[/php]

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

    [php]$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 for battlefield’;[/php]

    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

    snipe

    I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

    By snipe
    Snipe.Net Geeky, sweary things.

    About Me

    I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

    Get in Touch