2

我必须做一个非常简单的操作,但我的编程技能还不够。我必须在 Facebook 页面中计算喜欢并在我的网站上打印该数字。我有两个脚本可以很好地完成普通网站的工作,但它们不想显示页面的点赞数。

<?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));
?>

这两个脚本都适用于普通网站,但无法获取 fb 页面的点赞数。可能是我应该以其他格式输入链接还是什么?

我可以使用这样的图形获取所需的数据http://graph.facebook.com/?ids=AutoSpecCenter,只需输入这样的页面名称即可。但我不知道如何处理这些数据。

4

6 回答 6

7

正如您在问题中已经写的那样,您可以通过 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";
?>

如果您需要执行更多任务而不仅仅是获取页面之类的内容,请考虑按照cpilko的建议使用Facebook SDK

于 2012-08-27T19:11:13.740 回答
2

这是快速而肮脏的方法:

<?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);

你最好安装Facebook PHP SDK或使用 cURL 来获得它。

您也可以设置$fb_id为 url。

于 2012-08-27T19:06:49.263 回答
1

这也是一个简单的方法......

<?php $page_id = 'yourfbpageid'; // your facebook page id $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot"); $fans = $xml->page->fan_count; ?> <li class="text white"><span></span><?php echo $fans.' Fans'; ?></li>

礼貌:拉维帕特尔

于 2013-07-12T05:03:17.613 回答
1

这对我有用

<?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;

?>
于 2016-09-30T06:09:00.853 回答
0

我遇到了同样的问题,只是likes.summary(true),comments.summary(true)在“字段”中添加参数对我有用。

例如我用过https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_tags,likes.summary(true),comments.summary(true)

代替https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN

如果需要,您也可以添加其他参数;由 a 隔开,

此外,如果您想计算单个帖子,您可以使用

https://graph.facebook.com/POST_ID/likes?summary=true&access_token=ACCESS_TOKEN

喜欢计数

或者

    https://graph.facebook.com/POST_ID/comments?summary=true&access_token=ACCESS_TOKEN

评论数

于 2013-11-21T17:28:57.993 回答
0

试试这个代码

<?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;
    }
?>
于 2014-07-20T04:40:43.907 回答