如何将Facebook评论数保存到Mysql数据库中


How to save Facebook comments count to Mysql database?

我在我的网站上使用Facebook API允许用户在不同的页面上发表评论。

以下是如何获得特定页面的评论计数:

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

我想将这个值存储到MySQL数据库中,允许用户根据大多数Facebook评论进行排序。如果我能存储这个值,只有当一个人发表新的评论时,这将是最有效的。我该怎么做?(在FB文档中还没有发现任何有用的东西…)

Facebook提供了这一点,您可以使用FQL查询并从FQL表流中获取有关帖子的数据。

FQL查询

FQL表流

更新:

这段代码从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;
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

如果你想知道有多少评论,你应该能够使用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/