Home » Snippets » 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"
   }
}

Related Topics:

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
Snippets
Using Searchform to Search Keyword on WordPress Title only
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 apiIn 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... DEZZAIN.COM
Enjoy This Article?, Follow us or Share it smiley
viral and buzz WordPress theme

3 Comments Already

  1. Make sure your Access token App version is no higher than 2.8 or it will not work.

    Public CommentUserMake sure your Access token App version is no higher than 2.8 or it will not work.
  2. thanks,a lot its working …..

    Public CommentUserthanks,a lot its working .....
  3. it’s work for me.. thanks

    Public CommentUserit's work for me.. thanks

Write a Reply or Comment

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


You can wrap php, js or style css code with [PHP]..your code...[/PHP]