php 获取 Facebook Like 计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9255949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Facebook Like count
提问by Jimmy M
I have integrated Facebook Like into my website.Now i need to get/list the count of the Facebook like in the Admin panel of my website. Is there any way to get this done?Need suggestions.Thanks in advance.
我已将 Facebook Like 集成到我的网站中。现在我需要在我网站的管理面板中获取/列出 Facebook Like 的计数。有什么办法可以做到这一点?需要建议。提前致谢。
回答by enzyme42
$url = 'http://graph.facebook.com/PAGE_OR_APP_ID';
echo '['.$url.']: '.json_decode(file_get_contents($url))->{'likes'};
回答by Ajeet Sinha
In order to get the number of likes of any object using Facebook API, use a Facebook API of your choice and commands like this:
为了使用 Facebook API 获取任何对象的点赞数,请使用您选择的 Facebook API 和如下命令:
https://graph.facebook.com/< your object id>/
You will receive a JSON object and you can extract the number of likes from it:
您将收到一个 JSON 对象,您可以从中提取喜欢的数量:
{
"id": "567454",
"link": "http://www.facebook.com/pages/PHP-Developer/103146756409401",
"likes": 250,
"type": "page"
}
More info on https://developers.facebook.com/docs/reference/api/
有关https://developers.facebook.com/docs/reference/api/ 的更多信息
The more direct / updated link for this topic on facebook is
facebook 上此主题的更直接/更新的链接是
回答by krishna singh
function fbLikeCount($appid,$appsecret){
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$appid.'?access_token='.$appsecret;
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
return $likes = $json_output->likes;
}else{
return 0;
}
}
}
Facebook - <?php echo fbLikeCount('app/page id here','token here'); ?>
回答by Samuel Guebo
You can achieve that using the Graph API (v3)
您可以使用 Graph API (v3) 实现这一点
function checkFacebookReactions($url){
$access_token = 'YOUR-FACEBOOK-TOKEN'; // Generate a Facebook Token first
$api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
$fb_connect = curl_init(); // initializing
curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $fb_connect ); // connect and get json data
curl_close( $fb_connect ); // close connection
$body = json_decode( $json_return );
// Print each key values, if needed
foreach($body->engagement as $k=>$v){
echo $k .": " .$v ."<br>";
}
$count = $body->engagement->reaction_count; // Return only reactions (likes + other reactions)
return $count;
}
$url = "REPLACE-WITH-YOUR-URL"; // The url you want to fetch details from
checkFacebookReactions($url);
First, you will need to generate a Facebook token. You can fin plenty of resources that explain how to get a token. This one will do the trick: https://elfsight.com/blog/2017/10/how-to-get-facebook-access-token
首先,您需要生成 Facebook 令牌。您可以找到大量解释如何获取令牌的资源。这个可以解决问题:https: //elfsight.com/blog/2017/10/how-to-get-facebook-access-token