2014-01-26 32 views
1

我目前得到的數通過下面的PHP腳本喜歡的URL的通過PHP獲得一個URL的喜歡的數量不包括註釋部分接收喜歡的數量

<?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的數量,但我也得到了喜歡在評論部分(fb腳本)收到的數量。

我目前管理web大賽的基礎上,數目喜歡獲得了個人資料網址,以及一些用戶使用此方案(通過給很多喜歡通過他們的個人資料收到的意見),以增加他們的贏的機會。

所以,我怎麼能得到的實際數目喜歡的URL,在不包括喜歡在註釋框部分職位收到

謝謝, 亞歷克斯

回答

0

您可以嘗試使用Facebook的開放圖譜獲得的號喜歡

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

另一個更精確的方法是用FQL查詢:

Facebook Query Language Reference

  1. link_stat表,使用URL - link_stat Reference

    SELECT like_count, comments_fbid FROM link_stat WHERE url = "http://www.url.com/page" 
    
  2. 獲取likes使用comments_fbidobject_idcomment每個評論 - comments Reference

    SELECT likes FROM comment WHERE object_id = [comments_fbid] 
    
  3. 總和所有likes從每一個意見和。減去這總計到like_count

+0

開放圖表顯示完全一樣的結果我的代碼以上。 FQL在讓facebook通過我的應用程序訪問我的詳細信息後返回到同一頁面。你能發佈一個完整的代碼示例嗎? –

+0

閱讀Facebook SDK的[實現指南](https://developers.facebook.com/docs/php/gettingstarted),並在[Facebook Graph API Explorer]上嘗試查詢(https://developers.facebook.com/tools /資源管理器/)。尼斯回答順便說一句:P –

1

好吧,這是怎麼弄的實際數目喜歡的URL(不包括註釋,評論喜歡,股吧):

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