How to Use New Facebook API 2.7 for Fan Page and Post Share Count

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
[php]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;
}[/php]

How to use
[php]<?php echo dez_get_fb_fans_count(‘dezzain’,’147880095xxxxxxx’,’xxxxxxxxxx831fbc2557cfb89f021′); ?>[/php]

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

2. Get Post Share Count

The PHP Code
[php]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’]);
}
}[/php]

How to use
[php]<?php echo dez_get_post_share_count(‘website-reviews/top-5-free-web-hosting-sites-with-no-ads/’,’147880095xxxxxxx’,’xxxxxxxxxx831fbc2557cfb89f021′); ?>[/php]

or

[php]<?php echo dez_get_post_share_count( get_permalink() ,’147880095xxxxxxx’,’xxxxxxxxxx831fbc2557cfb89f021′); ?>[/php]

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"
   }
}
Scroll to Top