我在我的网站上使用Facebook API,允许用户在不同的页面上发表评论。
这是如何获取特定页面的评论数:
<fb:comments-count href='LINK_TO_PAGE'></fb:comments-count>
我想将此值存储到MySQL数据库中,允许用户按大多数Facebook评论进行排序。如果我可以存储这个值,只有当一个人发表新评论时,这将是最有效的。我怎样才能做到这一点? (还没有在FB文档中找到任何有用的东西......)
答案 0 :(得分:1)
Facebook提供此功能,您可以使用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;
答案 1 :(得分: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
答案 2 :(得分: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/