» » You are reading »

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

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"
   }
}
http://cdn.dezzain.com/1/2016/08/fb-app-panel-768x433.pnghttp://cdn.dezzain.com/1/2016/08/fb-app-panel-150x150.pngRichie KS Snippetsfacebook api
In 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...

Leave a Reply

Your email address will not be published. Required fields are marked *