Facebook 如何计算FB页面上的喜好?
我必须做一个非常简单的操作,但我的编程技能还不够。我必须在Facebook页面上计算喜欢的人数,然后在我的网站上打印这个数字。我有两个脚本可以很好地完成普通网站的工作,但它们不想显示页面的喜欢数量Facebook 如何计算FB页面上的喜好?,facebook,facebook-like,Facebook,Facebook Like,我必须做一个非常简单的操作,但我的编程技能还不够。我必须在Facebook页面上计算喜欢的人数,然后在我的网站上打印这个数字。我有两个脚本可以很好地完成普通网站的工作,但它们不想显示页面的喜欢数量 <?php $source_url = "http://www.facebook.com/"; //This could be anything URL source including stripslashes($_POST['url']) $url = "http://api.faceb
<?php
$source_url = "http://www.facebook.com/"; //This could be anything URL source including stripslashes($_POST['url'])
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$likes = $xml->link_stat->like_count;
$comments = $xml->link_stat->comment_count;
$total = $xml->link_stat->total_count;
$max = max($shares,$likes,$comments);
echo $likes;
?>
<?php
$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = 'http://www.apple.com/'";
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$json=file_get_contents($apifql);
print_r( json_decode($json));
?>
link\u stat->like\u count;
$comments=$xml->link\u stat->comment\u count;
$total=$xml->link\u stat->total\u count;
$max=max($shares,$likes,$comments);
回声$likes;
?>
这是一条又快又脏的路:
<?php
$fb_id = 'AutoSpecCenter';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
printf("<p>There are %s people who like %s</p>", $result->likes, $result->name);
正如您在问题中所写的,您可以通过Facebooks的Graph API查询此类信息。这个简短的示例将获取可口可乐页面的信息,解码JSON并输出喜欢该页面的人数$data->likes
<?php
$ch = curl_init("https://graph.facebook.com/CocaCola?access_token=<Access Token>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
echo $data->likes . " people like Coca-Cola";
?>
这里还有一个简单的方法
page->fan\u count;
?>
礼貌:拉维·帕特尔(ravi patel)我也遇到了同样的问题,只是在“字段”中的参数中添加了likes.summary(true)、comments.summary(true)
e、 g.我使用了https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_标记,likes.summary(true),comments.summary(true)
而不是https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN
如果需要,还可以添加其他参数;由一个
此外,如果你想单后计数,你可以使用
https://graph.facebook.com/POST_ID/likes?summary=true&access_token=ACCESS_TOKEN
因为喜欢很重要
或
有关注释计数,请尝试以下代码
<?php echo facebooklike('209414452444193');
function facebooklike($page_id){
$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
return $likes;
}
function get_contents($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
这对我很有效
<?php
function getData($username){
$access_token = 'YOUR ACCESS TOKEN'; //Replace it with your Access Token
$json = json_decode(file_get_contents("https://graph.facebook.com/".$username."?fields=fan_count&access_token=".$access_token),true);
return $json;
}
$data = getData("Mypage");//Replace it with your Username
$likes = $data['fan_count'];
echo "Facebook Likes: ".$likes;
?>
所以你不知道如何解析graph.facebook.com上的响应?实际上是:/。我知道这可能很琐碎,所以我有点为我的问题感到羞愧。file\u get\u contents
不适用于图形API;-)您必须使用cURL或其他工具。您好,这不起作用,query返回一个错误消息请求此资源需要一个访问令牌。
因为自从我在2012年和现在的回答以来,Facebook更改了API,现在需要一个访问令牌,添加一个访问令牌,它应该可以再次工作。
<?php
function getData($username){
$access_token = 'YOUR ACCESS TOKEN'; //Replace it with your Access Token
$json = json_decode(file_get_contents("https://graph.facebook.com/".$username."?fields=fan_count&access_token=".$access_token),true);
return $json;
}
$data = getData("Mypage");//Replace it with your Username
$likes = $data['fan_count'];
echo "Facebook Likes: ".$likes;
?>