fb-app-panelIn my previous Facebook snippet tutorial, i’ve written an easy PHP code to extract fan page count without access token. However this code will not work after the Facebook API changes in version 2.7, here’s the solution.

Important: You must have app ID and app SECRET for this code to work.

1. Get Facebook Fan Page Count

The PHP Code

function dez_get_fb_fans_count($fbid,$app_id,$app_secret) {
$urls = 'https://graph.facebook.com/v2.7/'. $fbid . '?fields=fan_count&access_token='. $app_id . '|' . $app_secret;
$string = @file_get_contents( $urls );
if($string) {
$fan_count = json_decode( $string );
$get_fan_count = $fan_count->fan_count;
return $get_fan_count;
}

How to use

<?php echo dez_get_fb_fans_count('dezzain','147880095xxxxxxx','xxxxxxxxxx831fbc2557cfb89f021'); ?>

this will return the number of fan in your Facebook page.

2. Get Post Share Count

The PHP Code

function dez_get_post_share_count($url,$app_id,$app_secret) {
$urls = 'https://graph.facebook.com/v2.7/?id='. urlencode($url) . '&access_token='. $app_id . '|' . $app_secret;
$string = @file_get_contents( $urls );
if($string) {
$fan_count = json_decode( $string,true );
return intval($fan_count['share']['share_count']);
}
}

How to use

<?php echo dez_get_post_share_count('http://www.dezzain.com/website-reviews/top-5-free-web-hosting-sites-with-no-ads/','147880095xxxxxxx','xxxxxxxxxx831fbc2557cfb89f021'); ?>

or

<?php echo dez_get_post_share_count( get_permalink() ,'147880095xxxxxxx','xxxxxxxxxx831fbc2557cfb89f021'); ?>

this will return the number of share for the post.

Without access token, you will encounter error code like this

{
   "error": {
      "message": "An access token is required to request this resource.",
      "type": "OAuthException",
      "code": 104,
      "fbtrace_id": "DN/rgaNjutw"
   }
}

Related Articles:

Snippets
How To Fix Chinese Language WordPress Excerpts Issue
Snippets
How to Get Facebook Page Like Count Without Access Token
Snippets
Different Post Count in WordPress Search and Archive Page