通过PHP获取URL的喜欢数量,不包括评论部分中收到的喜欢的数量

时间:2014-01-26 20:18:31

标签: php facebook

我目前通过以下PHP脚本获取网址的的数量:

<?php
$source_url = "[my-url-here]";  //This could be anything URL source including stripslashes($_POST['url'])

$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);

$shares =  $xml->link_stat->share_count;
$likes =  $xml->link_stat->like_count;
$comments = $xml->link_stat->comment_count;
$total = $xml->link_stat->total_count;
$max = max($shares,$likes,$comments);


echo "$source_url
<br><br>shares: $shares
<br><br>likes: $likes
<br><br>comments: $comments
<br><br>total: $total
<br><br>max: $max
<br>------------<br>
";

?>

问题在于likes: $likes,我不只是收到了网址的的数量,但我也计算了赞的数量在评论部分收到(fb脚本)。

我目前正在根据收到的个人资料网址的数量来管理网络竞赛,而且有些用户正在使用此方案(通过向赞成提供许多赞成他们的个人资料收到的评论),以增加他们获胜的机会。

那么,我如何才能获得网址的的实际数量,而不包括评论框部分中帖子收到的

谢谢, 亚历

2 个答案:

答案 0 :(得分:1)

好的,这是如何获取网址的实际数量(不包括评论,评论,分享):

  $source_url="http://www.google.ro"; //the url you need

  //first get the total number of likes, and the comments_fbid that we will need in the next query

  $fql_query_url = 'https://graph.facebook.com/'
  . 'fql?q=SELECT+like_count,comments_fbid+FROM+link_stat+WHERE+url="' . $source_url . '"';
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);


  //place them in 2 variables to use
  $like_total=$fql_query_obj["data"][0]["like_count"];
  $object_id=$fql_query_obj["data"][0]["comments_fbid"];

  echo $like_total;
  echo "<br>";
  echo $object_id;


   //execute next query from the *comment* table to get the array of *likes* in the comment section
   $fql_query_url = 'https://graph.facebook.com/'
    . 'fql?q=SELECT+likes+FROM+comment+WHERE+object_id=' . $object_id;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

//sum out the results in the array
$nr=count($fql_query_obj["data"]);  
  $s=0;
  for($i=0;$i<$nr;$i++)
  { $s+=$fql_query_obj["data"][$i]["likes"];   }


$therealnumberoflikes=$like_total-$s;
echo $therealnumberoflikes;

答案 1 :(得分:0)

您可以尝试使用Facebook Open Graph获取的数量:

http://graph.facebook.com/?ids=http://www.url.com/page

另一种更精确的方法是使用FQL查询:

Facebook Query Language Reference

  1. link_stat表格,使用网址 - link_stat Reference

    SELECT like_count, comments_fbid FROM link_stat WHERE url = "http://www.url.com/page"
    
  2. 使用likes作为comment - comments Referencecomments_fbidobject_id的每条评论获取SELECT likes FROM comment WHERE object_id = [comments_fbid]

    likes
  3. 对每条评论中的所有like_count求和,并将该总和减去{{1}}