How to: Display Facebook Page Fans count on your blog/website

Do you have Facebook page for your business? Did you ever tried to display your Facebook fans count on your blog/website? Some developers, using heavy plugins to display their Facebook fans count on their website. I really don’t like to install those heavy plugins to do little things like this. So decide to create a simple script for display the fans count. Yeah, its’s done. This code is very simple. I think, it’ll be very helpful for you.

step 1: Adding code to your site

We use the following php function to retrieve your Facebook fans count. Copy and paste it your common functions coded file like config.php, main_functions.php, engin.php etc..

For an example: WordPress uses the function.php file to write common custom functions for particular themes. If you are a WordPress develpoer open functions.php, paste this script and save the file.

<?php
function fb_count($value='') {
$url='http://api.facebook.com/method/fql.query?query=SELECT fan_count FROM page WHERE';
if(is_numeric($value)) {
$qry=' page_id="'.$value.'"';
} //If value is a page ID 
else {
$qry=' username="'.$value.'"';
} //If value is not a ID. 
$xml = @simplexml_load_file($url.$qry) or die ("invalid operation"); 
$fb_count = $xml->page->fan_count;
return $fb_count;
}
?>

step 2: Display Followers count

Now simply call that function any where you want show your fans count on your site/blog. You can use your Facebook Page ID or UserName as function’s input value. checkout the examples below and also try the live demo.

Function calling examples

  1. Input as Page IDfind-facebook-page-id
    Facebook Likes:<?php echo fb_count('114877608587606');?>
  2. Input as Page UserNamefind-facebook-page-user-name
    Facebook Likes: <?php echo fb_count('google');?>

It’s very simple, right? If you like this script don’t forget to share with your friends also learn How to: Display twitter followers count on your blog/website If you have any bug report, please share it below.

About

Find me on Google+, Twitter and Facebook.
%d bloggers like this: