问题
I'm trying to display Facebook comment counts in <div id="comments">
It has to be via Facebook Query Language (FQL). This post is almost exactly what I need: Facebook Graph Api url comments and shares count doesn't work anymore
But how do I display the comment_count
(from the query) into a div
? i.e. how do I process that data? So far, I have:
$(function(){
$.ajax({
url: 'https://graph.facebook.com/fql?q=SELECT%20comment_count%20FROM%20link_stat%20WHERE%20url=%27e',
dataType: 'jsonp',
success: function(data) {
if(data.comment_count)
{
$('body').find('#comments').html('Comments ('+jsonp.data.comment_count+')');
}else{
$('body').find('#comments').html('Comments (0)');
}
}
});
});
回答1:
I did it like this to update my div with the likes count like this
$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = '".$url."'";
$j.ajax({
url: 'https://api.facebook.com/method/fql.query?format=json&query=<?php echo urlencode($fql);?>',
dataType: 'jsonp',
success: function(data)
{
$j(".comment_count").html(data.comment_count);
}
});
works for me like a charm.
回答2:
For my part,
I used php code to get the comment count via fql. First, you need to download the facebook php sdk and load it at the top of your page:
require_once("src/facebook.php");
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_SECRET_KEY',
);
$facebook = new Facebook($config);
Then, the fql query:
$url = 'http://www.yoururl.com/;
$fquery = 'SELECT comment_count, share_count, like_count FROM link_stat WHERE url = "'.$url.'"';
$fparam = array( 'method' => 'fql.query', 'query' => $fquery );
$fql = $facebook->api($fparam);
$cmcount = $fql[0]['comment_count'];
So, $cmcount is now your comment counts, put it directly in your html code:
<div id="comments">
<?php echo $cmcount; ?>
</div>
来源:https://stackoverflow.com/questions/14371949/facebook-api-comment-count-via-fql