How to Get Facebook Like Count With PHP? (Updated)

This is an updated and working code snippet which can be used to retrieve Facebook like count of a specific page. To use this code snippet you’ll need a Facebook App ID and Secret Key.

In order to get an App ID and Secret Key, you have to create a new Facebook application.

Quick Tip : Extract the archive contents with PHP

Here’s a Quick Tutorial Which help you with that,

http://coffeecupweb.com/how-to-get-an-app-id-and-secret-key-from-facebook/

To get Facebook page like count use the following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret;
	$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 Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_secret_key__');

To extract the like count you need to page id or name, app id and secret key.

Facebook Graph API 2.4 Update:

The above code is working for apps below 2.4 version, but for apps above 2.4 you need to use following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret.'&fields=likes';
	$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 Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_app_secret__');

As you can see, with Graph API 2.4 we need to pass name of fields we need to access. Apart from likes you can also access,

  1. id
  2. about
  3. birthday
  4. category
  5. likes
  6. link
  7. name
  8. talking_about_count
  9. username
  10. website
  11. were_here_count

14 Comments

  1. Hi, nice tutorial, it worked for me for few weeks, but now I see that FB broke this method too :/

    {
    “error”: {
    “message”: “(#15) This method cannot be called with a client token or an app secret embedded in the client code”,
    “type”: “OAuthException”,
    “code”: 15
    }
    }

    Reply
  2. Hi,

    This code is not working for newly created app v2.4.

    It just returns

    stdClass Object
    (
    [name] => Page Name
    [id] => Page Id.
    )

    You have any idea about this issue.

    Thanks.

    Reply
  3. Nice example.. since 2.6 API you need to use fan_count as a field property:

    $json_url =’https://graph.facebook.com/’.$id.’?access_token=’.$appid.’|’.$appsecret.’&fields=fan_count’;

    took me a couple of hours to find out..

    Reply
  4. What would be the php code to fetch facebook likes of a particular url after all the graph api have been deprecated. Any help is much appreciated.

    Reply

Leave a Comment.