2012-02-19 90 views
3

我在我的網站上使用Facebook API來允許用戶在不同的頁面上發表評論。如何將Facebook評論保存到Mysql數據庫?

這是如何獲得的評論數爲特定頁面:

<fb:comments-count href='LINK_TO_PAGE'></fb:comments-count> 

我想這個值存儲到一個MySQL數據庫,使用戶可以通過Facebook的多數意見進行排序。如果我可以存儲這個值,那麼只有當一個人發表新評論時纔是最有效的。我怎樣才能做到這一點? (在FB文檔中沒有發現任何有用的東西...)

回答

1

Facebook提供此功能,您可以使用FQL查詢並從FQL表流中獲取有關帖子的數據。

FQL Query

FQL Table stream

更新:在http://www.facebook.com/facebook

從後

此代碼抓取數據,需要用戶的訪問令牌運行FQL查詢。

$post_id= "20531316728_10150867335071729"; 
$access_token= YOUR_ACCESS_TOKEN; 

$query= "SELECT post_id, message, comments FROM stream 
    WHERE post_id= '" . $post_id . "'"; 
// Run fql query (Output: XML) 
$fql_query_url = 'https://api.facebook.com/method/fql.query?query=' . urlencode($query) . '&access_token=' . $access_token 

$ch= curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $fql_query_url); 
$curl_data= curl_exec($ch); 
curl_close($ch); 

$data= simplexml_load_string($curl_data); 
echo "<pre>"; 
print_r($data); 
echo "</pre>"; 
exit; 
+0

看起來很有希望。雖然有很多的閱讀,你能提供一個例子,尤其是收集評論數量嗎? – 2by 2012-02-19 22:16:43

0

如果你想知道有多少評論存在,你應該可以從服務器使用PHP來完成它,你可以在這個主題中看到更多關於它的信息。 :http://facebook.stackoverflow.com/questions/4791951/retrieve-facebook-post-comments-using-graph-api

如果你想要做檢查計數當過用戶添加一個新的評論,那麼你就需要使用Facebook的JavaScript SDK來訂閱事件,像:

FB.Event.subscribe("comment.create", 
    function(response) { 
     // make ajax request to your server to update the count 
    } 
); 

你可以在這裏閱讀更多關於此:http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

1
FB.Event.subscribe('comment.create', 
function(response) { 
onCommentCreate(response.commentID,response.href); //Handle URL on function to store on database 

alert(response.href); //it gives you url 
} 
); 

function onCommentCreate(commentID,href) { 
    $.ajax({ 
     type: 'POST', 
     url: 'handlecomment.php', 
     data: {commentid:commentID,href:href}, 
     success: function(result) 
     { 
     alert(result); 
     } 
    }); 
} 

//hadlecomment.php 


<?php 
error_reporting(E_ERROR); 
    $commentid=$_POST['commentid']; 
    $url=$_POST['href']; 
    $pid=substr($url,strpos($url, 'comments')+8); 

// Remember to copy files from the SDK's src/ directory to a 
    // directory in your application on the server, such as php-sdk/ 
    require_once('php-sdk/facebook.php'); 

    $config = array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_APP_SECRET', 
); 

    $facebook = new Facebook($config); 
    $user_id = $facebook->getUser(); 
$accesstoken=$facebook->getAccessToken(); 

if($user_id) { 

    // We have a user ID, so probably a logged in user. 
    // If not, we'll get an exception, which we handle below. 
    try { 
     $facebook->setAccessToken($accesstoken); 
     $fql = 'SELECT text from comment where id = ' . $commentid; 
     $ret_obj = $facebook->api(array(
            'method' => 'fql.query', 
            'query' => $fql,)); 
$comment= $ret_obj[0]['text'] ; 
    $insert_comment="insert into comments(pid,comment) values($pid,$comment)"; 
mysql_query($insert_comment); 


    } catch(FacebookApiException $e) { 
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid. 
    // In this case, we'll get an exception, so we'll 
    // just ask the user to login again here. 
    $login_url = $facebook->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>'; 
    error_log($e->getType()); 
    error_log($e->getMessage()); 
    } 
} else { 

    // No user, so print a link for the user to login 
    $login_url = $facebook->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>'; 

} 
    ?> 

?> 


//YOu need to set data-href of comment should be look like this... 
//i am using more comments on my website so i looped through to add comment 


while($result=mysql_fetch_assoc(mysql_query($query))) 
{ 
    $pic_id=$result['pic_id']; // i have saved unique pic id in my database for all images so i am  

//retrieving that here 

<div class="fb-comments" style=' position:relative;left:55px;top:10px;' data-href="<?php echo 'http://www.lpuphotography.edulogics.in/votography.php/comments' . $pic_id; ?>" data-width="470" data-num-posts="2"></div> 

} 

//if you are using single comment 

<div class="fb-comments" style=' position:relative;left:55px;top:10px;' data-href="<?php echo 'http://www.lpuphotography.edulogics.in/votography.php/comments101' ?>" data-width="470" data-num-posts="2"></div> 

//101 is comment id , u can set what ever you want