Friday 13 June 2014

Get Facebook Likes Count of a page using Graph API

This small code snippet given below uses FaceBook’s graph api and returns the likes count of the page. There is no need to have the FaceBook access token to call the api and get the result.
<?php
//The following code returns the Number of likes for any facebook page.
 
//Page Id of Knowledgecotner Replace it with your page.
$page_id = "556271807742934";
$likes = 0; //Initialize the count
 
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
 
//Extract the likes count from the JSON object
if($json_output->likes){
    $likes = $json_output->likes;
}

//Printing the count
echo $likes.' Facebook Fans';
?>