Facebook API - comment count via FQL
Asked Answered
P

2

1

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)');
   }
  }
 });
});
Pemmican answered 17/1, 2013 at 3:51 Comment(1)
Hi! - the url is just an example: a complete url would be, for instance: graph.facebook.com/…Pemmican
D
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.

Deforce answered 17/1, 2013 at 7:13 Comment(3)
so you're saying all I need to do is replace -- url = '".$url."'"; with my url? e.g. (url='google.com')? (Tried that, it didn't work. Thank you!Pemmican
basically, to clarify, I need to display in a div the comment_count on this example link: graph.facebook.com/…Pemmican
The .comment_count is the class of the div and it will replace the html of div with the comment count.Deforce
D
1

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>
Derry answered 4/2, 2013 at 14:36 Comment(1)
Try 'SELECT commentsbox_count' instead of 'SELECT comment_count', it's more accurate!Derry

© 2022 - 2024 — McMap. All rights reserved.