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



我当前正在通过以下php脚本获得URL的 like 的数量:

<?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,我不仅收到URL的 like 的数量,但我也被计数评论部分(FB脚本)。

我目前正在管理一个网络竞赛,基于收到的profile URL的 like ,一些用户正在使用此方案(通过将许多 Likes 提供给他们的个人资料收到的评论)增加了他们的胜利机会。

那么,如何获得URL的实际数量,而不包括帖子在"评论"框部分中收到的 likes

谢谢Alex

好吧,这是获取 url likes 的实际数量(排除评论,评论,喜欢,分享):

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

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

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

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

Facebook查询语言参考

  1. 来自link_stat表,使用URL -link_stat参考:

    SELECT like_count, comments_fbid FROM link_stat WHERE url = "http://www.url.com/page"
    
  2. 使用comments_fbid作为object_idcomment评论获取likes - 注释参考:

    SELECT likes FROM comment WHERE object_id = [comments_fbid]
    
  3. 总和每个评论中的所有likes,并提取总计like_count

最新更新