2015-04-17 76 views
11

以前我使用的是FQL,但是从v2.1开始不推荐使用,我正在使用图形边缘移动到v2.3“likes ”。请求获取v2.3中Facebook页面喜欢的总数API

这里是我的网址:

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

这将返回详细JSON与寻呼信息 - 但它忽略了TOTAL_COUNT这是作为Facebook docs描述应该是在返回时“总结=真正的”使用 - 你会明白我的意思。

回答

31

任何磕磕绊绊到这个答案现在(2016年4月),将获得沮丧,因为公认的答案不再V2.6

?场=喜欢工作/喜欢现在返回相同的结果 - >页面喜欢的页面。

为了得到粉丝数,你现在需要使用栏= fan_count

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

正如你可以在上面看到,你也可以直接与页面名提出请求,无需取的pageID。

+3

正确,我已经更新这是现在正确的答案。 –

+2

我如何获得此网址的正确访问标记? –

+2

你是一个拯救生命的人。这在他们的文档中没有任何地方! – jetlej

5

您在寻找喜欢该页面的人的总数量还是页面喜欢的内容?

例如。

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

将返回Page PepsiUS喜欢什么。

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

将返回喜欢该页面的总人数。

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

了验证这里PepsiUS

+2

这是第二个,即?fields = likes。 –

+0

完美,有效 - 非常感谢。 –

+0

我相信不同之处在于PageID/Likes是将返回目标页面喜欢的其他页面的Endpoint。 PageID?fields = Likes是对页面/用户的边数进行计数。 –

0

感谢@NativePaul

我花了差不多两天找到一个解决方案,以获得Facebook专页计数器喜欢在数值的简码。所以,我已经修改了我从这个环节得到了代码:http://www.internoetics.com/2015/07/13/display-number-facebook-page-likes-wordpress-php/

和修正其与fan_count领域工作,这是给你参考代码:

/* 
 
\t Display the Number of Facebook Page Likes in Plain Text with WordPress Shortcode (and PHP) 
 
\t 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');

你有将上面的代码添加到主题函数文件中,并在代码开头提到的任何地方使用Shortcode。

相关问题