Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request to get total count of Facebook page likes in v2.3 API

Previously I was using FQL for this, but this is deprecated as of v2.1 and I'm moving over to v2.3 using the graph edge "likes".

Here is my URL:

https://graph.facebook.com/v2.3/<page_id>/likes?access_token=<access_token>&summary=true

This returns detail JSON with paging info - but it omits the total_count which is supposed to be returned when "summary=true" is used as described in the Facebook docs - you'll see what I mean.

like image 881
James Harcourt Avatar asked Apr 17 '15 14:04

James Harcourt


3 Answers

Thanks @NativePaul

I spent almost two days to find a solution to get the Facebook Fan Page likes counter in numeric value to a shortcode. So I have amended a code I got from this link: http://www.internoetics.com/2015/07/13/display-number-facebook-page-likes-wordpress-php/

And amended it to work with the fan_count fields and here is the code for your reference:

/*
	Display the Number of Facebook Page Likes in Plain Text with WordPress Shortcode (and PHP)
	Shortcode: [fbpagelikes id="" appid="" appsecret="" cache="" n="1"]
*/


function internoetics_fb_pagelikes($atts) {
  extract(shortcode_atts(array(
    'id' => 'kenryscom',
    'appid' => 'xxxxxxxxxxxxxxxx',
    'appsecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'n' => 1,
    'cache' => 3600 * 24 * 1
  ), $atts));

 $fbcounthash = md5("$url.$cache.$appid.$appsecret.$n");
 $fbcountrecord = 'fblikes_' . $fbcounthash;
 $cachedposts = get_transient($fbcountrecord);
 if ($cachedposts !== false) {
 return $cachedposts;

  } else {

  $json_url ='https://graph.facebook.com/' . $id . '?fields=fan_count&access_token=' . $appid . '|' . $appsecret;
  $json = file_get_contents($json_url);
  $json_output = json_decode($json);
 
  if($json_output->fan_count) {
   $fan_count = $json_output->fan_count;
   if ($n) $fan_count = number_format($fan_count);
   set_transient($fbcountrecord, $fan_count, $cache);
   return $fan_count;
    } else {
   return 'Unavailable';
  }
 }
}
add_shortcode('fbpagelikes','internoetics_fb_pagelikes');

You have to add the above code to the theme functions file and use the Shortcode anywhere as mentioned in the beginning of the code.

like image 170
Michael Henry Avatar answered Oct 27 '22 00:10

Michael Henry


Anyone stumbling onto this answer now (April 2016) will get frustrated because the accepted answer no longer works in v2.6

?fields=likes and /likes now return the same result -> the pages that the page likes.

To get the number of fans, you now need to use fields=fan_count

https://graph.facebook.com/pepsius/?fields=fan_count&access_token=<access_token>

As you can see above, you can also make the request directly with the pagename, no need to fetch the pageID.

like image 38
NativePaul Avatar answered Oct 27 '22 02:10

NativePaul


What you are looking for the total number of people that have liked the page or what the page has liked?

For example.

https://graph.facebook.com/v2.3/56381779049/likes?access_token=<access_token>&summary=true

Will return what the Page PepsiUS has liked.

https://graph.facebook.com/v2.3/56381779049?fields=likes&access_token=<access_token>

Will return the total number of people that have liked the page.

{"likes": 32804486, 
"id": "56381779049"}

Varified here PepsiUS

like image 24
Frank D Avatar answered Oct 27 '22 01:10

Frank D