调用Facebook API以回响计数不起作用 [英] Call Facebook API to echo like count doesn't work
问题描述
我想在我的网站上显示我的Facebook页面的点赞次数.我使用的以前的方法已经几天没用了.
I'd like to display the number of likes for my Facebook page on my website. The previous method I used isn't working anymore since a couple of days.
当我像这样调用Facebook graph API时: https://api .facebook.com/method/links.getStats?urls = http://www.facebook.com/549585444& format = json
When I call the Facebook graph API like this: https://api.facebook.com/method/links.getStats?urls=http://www.facebook.com/549585444&format=json
它给出了以下输出(虚构示例):
It gives me the following output (fictional example):
[{"url":"http:\/\/www.facebook.com\/549585444","normalized_url":"http:\/\/www.facebook.com\/549585444","share_count":0,"like_count":122,"comment_count":0,"total_count":122,"click_count":0,"comments_fbid":null,"commentsbox_count":0}]
现在我想回声类似的数字:
Now I like to echo the like count:
<?php
$fb_page = "549585444";
$url = "https://api.facebook.com/method/links.getStats?urls=http://www.facebook.com/".$fb_page."&format=json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$json_returned = curl_exec($curl);
curl_close($curl);
$json_returned = json_decode($json_returned, true);
echo $json_returned['like_count'];
?>
但是没有出现数字.知道为什么吗?
But the number doesn't appear. Any idea why?
推荐答案
正如您在JSON输出中所看到的,答案被包装在一个数组中:
As you can see in the JSON output, the answer is wrapped in an array:
array(1) {
[0]=>
array(9) {
["url"]=>
string(33) "http://www.facebook.com/549585444"
["normalized_url"]=>
string(33) "http://www.facebook.com/549585444"
["share_count"]=>
int(0)
["like_count"]=>
int(0)
["comment_count"]=>
int(0)
["total_count"]=>
int(0)
["click_count"]=>
int(0)
["comments_fbid"]=>
NULL
["commentsbox_count"]=>
int(0)
}
}
要获取数字输出,您必须通过更改以下行来获取数组中的第一项:
To get the number output, you'll have to get the first item in the array by changing this row:
echo $json_returned['like_count'];
对此:
echo $json_returned[0]['like_count'];
这篇关于调用Facebook API以回响计数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!