11

I need to fetch facebook likes,share, comments count from an article

Is there any way to fetch facebook (likes,share, comments) count.

Thanks in advance.

praneeth
  • 535
  • 1
  • 6
  • 17
  • atleast do a little bit research... – Valour May 26 '11 at 10:58
  • 3
    @Gokhan: asking someone to do it for you does not count as research? I'll make a note of that. – MJB May 26 '11 at 11:13
  • possible duplicate of [How to get share counts using graph api](http://stackoverflow.com/questions/5699270/how-to-get-share-counts-using-graph-api) – bkaid May 26 '11 at 14:42

4 Answers4

27

Actually you can have a more detailed report using FQL. Try following query:

  • SELECT url, normalized_url, share_count, like_count, comment_count, total_count, commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url = 'www.apple.com'

Here the php code:

$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 = 'www.apple.com'";

$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$json=file_get_contents($apifql);
print_r( json_decode($json));

And this is the expected result:

Array
(
    [0] => stdClass Object
        (
            [url] => www.apple.com
            [normalized_url] => http://www.apple.com/
            [share_count] => 355693
            [like_count] => 500374
            [comment_count] => 290890
            [total_count] => 1146957
            [commentsbox_count] => 2
            [comments_fbid] => 388265801869
            [click_count] => 16558
        )

)
freedev
  • 25,946
  • 8
  • 108
  • 125
5

This solution worked for me:

<?php
$source_url = "http://www.flightpodcast.com/episode-6-john-bartels-qantas-qf30";
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);

echo "Share --- ".$shares = $xml->link_stat->share_count;
echo "<br/>";

echo "Like --- ".$likes = $xml->link_stat->like_count;
echo "<br/>";

echo "Comments ---".$comments = $xml->link_stat->comment_count; 
echo "<br/>";

echo "Total --- ".$total = $xml->link_stat->total_count;
echo "<br/>";

echo $max = max($shares,$likes,$comments);
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
praneeth
  • 535
  • 1
  • 6
  • 17
  • This works great! I'm noticing something which has nothing to do with your code but with facebook: I am using the facebook comments plugin on a WordPress site. Facebook isn't counting the comments as actual "comments", but as "shares". Furthermore, deleting a comment from the facebook 'module' on the WP site, or from my facebook profile doesn't reduce the "shares" count... Going to also make a separate question on this one. – HandiworkNYC.com Jun 21 '11 at 22:28
  • 1
    Thanks a million. Your are using the "old" REST API. facebook writes on ther documentation site, that this is deprecated. Do you or somebody else know how to get these counters using the "new" Graph API? – Jan Deinhard Aug 10 '11 at 09:49
  • I think you can now only get likes with the Graph API. – Brian Ortiz Feb 23 '13 at 21:00
1
result = mysql_query($query);

 while($row = mysql_fetch_array($result))
 {
    $photoid = $row['photoid'];
    $likes = $facebook->api("/$photoid/likes");


    echo "<br><br>".$row['photoid'];
    echo "<br />";

    foreach ($likes['data'] as $like)
    {
        $likeid = $like['id'];
        $name =  $like['name'];
        echo "<br>like : ".$like['id']."  , ".$like['name'];

        $query = sprintf("INSERT INTO photo_like VALUES (%s,%s,'%s')",$user_id,$likeid,$name);

        echo "<br>query string : ".$query."<br><br>";
        $result = mysql_query($query);
        if (!$result) {
                            $message  = 'Invalid query: ' . mysql_error() . "\n";
                            $message .= 'Whole query: ' . $query;
                            echo "<br>";
                        }
love
  • 132
  • 1
  • 2
0

Using Graph API as FQL is deprecated, The 101.. you see in the url is the article id

https://developers.facebook.com/tools/explorer

enter image description here

vsingh
  • 6,365
  • 3
  • 53
  • 57