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.

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 '<li>'.$external_result[0]['like_count'].' likes';

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:

print_r($external_result);

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

Advertisement

Themeforest

Advertisement

468x60_makemoney
Previous post

Facebook Like-Gating in IFRAME Tabs

Next post

Facebook Custom Share Pop-Up in IFRAME Tabs Using jQuery and the Javascript SDK

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

  • Thank you, thank you, thank you.

    🙂

  • Very timely.
    Can you explain something for me?
    If I run a query to the fql api I get the following XML back.

    21
    34
    8
    0

    Two things…What’s “click_count”?

    Why don’t the like and share counts add up to the total_count?

    • Heya foamcow. Honestly, no idea why it’s not adding up. Do you get the same result when you use the PHP-SDK?

      The click_count number is supposed to be a count of the number of clicks on your like button, I think? But is very confusing. For that same snipe.net page, I get:

      [share_count] => 18
      [like_count] => 6
      [comment_count] => 9
      [total_count] => 33
      [click_count] => 2

      But certainly one would question how I could have 6 likes with only 2 clicks.
      https://www.facebook.com/snipe.net?sk=app_160853827313238

      The link_stats FQL docs make no mention of it, but the comments lead me to believe they used to. https://developers.facebook.com/docs/reference/fql/link_stat/

      Not sure if they’re deprecating it or if it’s an oversight. I’ll try to ask the PDC group.

  • Rafael Pellon

    Hi

    I’m a little frustated because I can’t find the solution of my problem in Facebook. I want to get using FQL or Open Graph the commens that a user makes in the wall of one of his friends but there isn’t any API to do it. Any idea?

    Thanks, your blog it is very interesting.

  • Any idea why the comment_count works and “SELECT count FROM comments_info WHERE app_id = ‘APPID’ AND xid=’XID’ ” doesn’t? There should be a count that takes a XID in comments_info which is the logical place to do it yet I always get an empty array when trying to make the above work…

    Though I’m relieved it works I’m a bit wary of using the link_stat because as of the official documentation ( http://developers.facebook.com/docs/reference/fql/link_stat/ ) it looks like it was made for the “Share” architecture and not necessarily the comment table of the Graph. Am I just over thinking this or is Fb really this complicated and annoying?

  • Hey! Can you give me an hint on how to use this to build something as a “FB page Likes progress bar” ?

    • Just set a config variable for what the limit should be and use the open graph like count ti determine how far along you are.

  • zain

    hi,

    i’m creating a Page Tab for a Facebook Fan Page, which pulls photos from the Fan Page’s Albums and displays them as thumbnails.

    What I am trying to do is when the user clicks on a thumbnail in my iFrame app, they get Facebook’s photo viewer overlay, with all the commenting/like/share functionality. (see attached screenshot).

    Is there any way to do this, using the FB.api?

    Please help.