Thursday, January 8, 2015

How to Fetch Facebook Share, Likes, Comments count from an Article using PHP

As we know that, now-a-days social media like, share and comment play a vital role to promote any article, post or web pages. It becomes a trend to show Facebook like, share and comment without using third party social plug in for the website pages. There are multiple ways to achieve this using PHP like Facebook query language and Facebook graph. Here, check out simple PHP code to fetch Facebook likes, share and comment count using PHP.



Method 1: Facebook Query Language  :-

<?php
 $source_url = 'https://www.facebook.com/webprogramminghub';
 $query_for_fql  = "SELECT share_count, like_count, comment_count FROM link_stat WHERE url = '".$source_url."'";
 $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($query_for_fbl);
 $response = file_get_contents($fqlURL);
 $json_data = json_decode($response);
 $fb_share_count = $json_data[0]->share_count;
 $fb_like_count = $json_data[0]->like_count;
 $fb_comment_count = $json_data[0]->comment_count;

        echo 'Facebook Share:'.$fb_share_count.'<br/>';
        echo 'Facebook Like:'.$fb_like_count .'<br/>';
        echo 'Facebook Comment:'.$fb_comment_count .'<br/>';
?>

Method 2: Facebook Graph API :-

<?php
 $source_url = 'https://www.facebook.com/webprogramminghub';
 $json_string = file_get_contents('http://graph.facebook.com/?ids=' . $source_url);
 $json = json_decode($json_string, true);
 fb_share_count = $json[$url]['shares'];

 $json_string = file_get_contents('http://graph.facebook.com/webprogramminghub’);
 $json = json_decode($json_string, true);
 $fb_like_count  = $json[$url]['likes'];

 echo 'Facebook Share:'.$fb_share_count.'<br/>';
 echo 'Facebook Like:'.$fb_like_count .'<br/>';

?>

0 comments:

Post a Comment