Counting Facebook Fans in PHP: The Graph API Way

In a previous blog post called How to Count Facebook Fans in PHP I’ve shown a code snippet of how to count the number of fans on a fan page using PHP. Times have changed, the Graph API has been introduced, and due to some responses I introduce here the new way of retrieving your fans count using the new Graph API and php.

Before you copy and paste, flush my comments with ‘my code is not working’ posts, I’d like to get your attention to versioning of the Facebook PHP SDK which we’ve been using all this time. The SDK has changed and of course the old method doesn’t work with the new SDK which is mostly tuned to Graph API, therefore, my previous code still works on a dozen on websites, because I have the old SDK installed back there. So please, be careful to what you download and use, read release notes and change logs, it will save you hours of googling.

The following snippet is based on the Facebook PHP SDK version 2.1.1 (use the Switch Tags option on github to browse through different tags). So get a fresh copy of facebook.php and have it somewhere nearby.

Unlike the old FBQL way, the new Graph API is much easier to work with, and retrieving the fans count is literally two lines of code (initialization doesn’t count). Here’s the snippet to retrieve the fans count for Mashable (don’t forget to replace your application ID and API secret):

require('facebook.php');

$facebook = new Facebook(array(
	'appId' => 'your-app-id',
	'secret' => 'your-api-secret',
));

$mashable = $facebook->api('/mashable');
echo 'Mashable has ' . $mashable['fan_count'] . ' fans';

Easy as that! I was also surprised to see that the Graph API is doing so well. Yeah, the documentation is not very rich, but whenever you need to retrieve something from Facebook, you can always print_r the results, which gives you the full picture. Sending data into Facebook is a little trickier and I’ll show you how in a later blog post.

Cheers!

18 thoughts on “Counting Facebook Fans in PHP: The Graph API Way

  1. How to Count Facebook Fans in PHP

  2. Facebook Fans Count Using Python and the Graph API

  3. Thank you for posting this! I have a problem that I have been looking EVERYWHERE for a solution to…

    We are running promotions based on the date of the "like". I am trying to run the following query but it throws a no-index exception.

    $result = $facebook->api(array(
    'method' => 'fql.query',
    'query' => 'SELECT uid, created_time FROM page_fan WHERE page_id = 169879363080;'
    ));

    Any insight into this would be awesome.

    • As of now, there is no way to run that query as Facebook has yet to make the "page_id" field indexable. Total B.S. if you ask me. I wasted 3 days trying to get this to work/looking for solutions.

    • Use this PHP function:
      number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

    • I got working :) Thanks kovshenin and Breklin, you are very helpfull, this is my final code, if some body need it:
      api('/metamorffosis');
      echo number_format($number['fan_count'], 0, '.', ',') . ' likes';
      ?>

  4. I got working :) Thanks kovshenin and Breklin, you are very helpfull, this is my final code, if some body need it:

    $number = $facebook->api('/metamorffosis');
    echo number_format($number['fan_count'], 0, '.', ',') . ' likes';

  5. I this is better code to count number of fans
    'your app id',
    'secret' => 'your app secret',
    '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 = your page id;'
    ));
    echo $fb_fans = $result[0]['fan_count'];
    ?>

    • Exelent! that way is better, this is my final code for separated Thousand of fans or likes with comma:

      echo $fb_fans = $result[0]['fan_count'];
      $result = $facebook->api(array(
      'method' => 'fql.query',
      'query' => 'select fan_count from page where page_id = your page id;'
      ));
      echo $fb_fans = number_format($result[0]['fan_count'], 0, '.', ',') . ' likes';

  6. Has anyone found a solution to tracking "likes" over a specific time frame? I have seen this done on facebook recently so I know it is possible. I am able to currently pull in the total number of likes with the graph api using the following:

    $json_url ="https://graph.facebook.com/155323844495680";
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);
    $likes = 0;
    if($json_output->likes){
    $likes = $json_output->likes;
    }

    • You might have to query facebook for the amount of likes every hour or day and store them in a database or something for historical data ;)

Comments are closed.