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

3

我在我的网站上使用Facebook API,让用户可以在不同的页面上发表评论。

以下是获取特定页面评论计数的方法:

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

我希望将这个值存储到MySQL数据库中,使用户可以按最多Facebook评论进行排序。如果只有在用户发表新评论时才存储该值,那么效率会更高。我该如何做?(在FB文档中还没有找到有用的信息...)

3个回答

1

Facebook提供了这个功能,您可以使用FQL查询并从FQL表stream中获取帖子数据。

FQL查询

FQL表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;

看起来很有前途。不过需要阅读很多内容,你能否提供一个示例,特别是收集评论计数的示例? - 2by

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

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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接