Snipe.Net Geeky, sweary things.

Getting Like, Share and Comment Count on External Pages in Facebook

G

In a previous post, I explained how to use an FQL query to get the current number of “likes” for a page, even if that page is age-gated. This time we’re going to look at the FQL query to do something similar for external pages (content pages not on Facebook, like blog posts, etc.)

For this demo, I’ll use the url of my primary site, Snipe.Net, with a url of http://www.snipe.net.

[php]require_once ‘facebook-php-sdk/src/facebook.php’;

// Create our Application instance.
$facebook = new Facebook(array(
‘appId’ => ‘YOUR APP ID’,
‘secret’ => ‘XXXXXXXXXXXXXXXXXXXXXXXXXX’,
‘cookie’ => true,
));

// run the FQL query for the content page
$external_result = $facebook->api(array(
‘method’ => ‘fql.query’,
‘query’ => ‘SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url=”http://www.snipe.net”;’
));
echo ‘

  • ‘.$external_result[0][‘like_count’].’ likes’;[/php]

    The “Like” is a Lie

    Something to bear in mind here – we’re querying for a few different numbers. The “like” number you see on the like widgets all over the internet is not the actual number of likes – it’s a combination of likes, shares and comment counts.

    To demonstrate this, let’s look at the output of that FQL query. If we add:

    [php]print_r($external_result);[/php]

    We get:

    Array
    (
    [0] => Array
    (
    [share_count] => 18
    [like_count] => 6
    [comment_count] => 9
    [total_count] => 33
    )
    )

    My like button says 33, but the actual like count is 6. But 18+6+9 = 33. So what you see on the like button is actually the total_count, not a true number of likes.

    From here, you can use those numbers directly as numbers, or you can display them as a string using number_format() to add commas (like 1,000,000).

  • 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