在 v2.3 中请求获取 Facebook 页面的总点赞数 API

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

以前我为此使用 FQL,但从 v2.1 开始不推荐使用,我正在使用图形边移至 v2.3 "likes"。

这是我的 URL:

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

这个 returns 详细信息 JSON 带有分页信息 - 但它省略了 total_count 应该在 [=22 时返回=] 按照 Facebook docs 中的描述使用 - 你会明白我的意思。

您要查找的是喜欢该页面的总人数或该页面喜欢的内容?

例如。

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

请问return PepsiUS 喜欢的页面。

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

将 return 喜欢该页面的总人数。

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

已在此处验证PepsiUS

现在(2016 年 4 月)遇到这个答案的任何人都会感到沮丧,因为接受的答案在 v2.6 中不再有效

?fields=likes/likes 现在 return 相同的结果 -> 页面喜欢的页面。

要获取粉丝数,您现在需要使用fields=fan_count

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

如上所示,您也可以直接使用pagename进行请求,无需获取pageID。

感谢@NativePaul

我花了将近两天的时间来找到一个解决方案,将 Facebook 粉丝专页的点赞计数器的数值转换为简码。所以我修改了从这个link得到的代码:http://www.internoetics.com/2015/07/13/display-number-facebook-page-likes-wordpress-php/

并将其修改为与 fan_count 字段一起使用,这里是供您参考的代码:

/*
 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');

您必须将以上代码添加到主题功能文件中,并在代码开头提到的任何地方使用简码。