How to get Facebook likes count of a page using Graph API

Due to change in Facebook API this code snippet doesn’t work any longer. Click Here for solution.

Facebook’s graph API is the simplest way to get the like count of a Facebook page. Using the following code snippet you can grab the like count of a particular Facebook page and display it on your website.

All you need to do is pass Username or id of the page,

//Get Facebook Likes Count of a page
function fbLikeCount($id){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'';
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);
 
	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb's Facebook page
echo fbLikeCount('coffeecupweb');

Useful Read: How to Add the Facebook Like Box / Fan Box in WordPress

7 Comments

    • You need to create a Facebook app and pass your app id and app secret with the URL.
      Replace the following line.
      $json_url =’https://graph.facebook.com/’.$id.”;
      with,
      $json_url =’https://graph.facebook.com/’.$id.’?access_token={your_app_id}|{your_app_secret}’;

      Reply

Leave a Comment.