IT Guides, Electronics, Reviews, and more!
IT Guides, Electronics, Reviews, and more!

PHP Display Counter for Facebook Likes, Shares, Comments

Ever wanted to display Facebook’s Likes, Shares, or Comments just as a numerical counter? Well now you can with this simple PHP tutorial.

With Facebook’s Legacy FQL API:

<?php
//The following code returns number of likes, shares, and comments for any Facebook page.
function facebook_count($url)
{
    // Query in FQL
    $fql  = "SELECT share_count, like_count, comment_count ";
    $fql .= " FROM link_stat WHERE url = '$url'";
    $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
 
    // Facebook Response is in JSON
    $response = file_get_contents($fqlURL);
    return json_decode($response);
}
 
// Replace your Facebook URL here
$fb = facebook_count('https://www.facebook.com/egadgets4me');
 
//Facebook share count
echo("Share Count: {$fb[0]->share_count}<br>");
 
//Facebook like count
echo("Like Count: {$fb[0]->like_count}<br>");
 
//Facebook comment count
echo("Comments Count: {$fb[0]->comment_count}<br>");
?>

Or with Facebook’s Graph API:

<?php
//The following code returns the Number of likes for any Facebook page.   
//Page Id of eGadgets4me. Replace it with your page.
$page_id = "1472477349644361";  
$likes = 0; //Initialize the count   

//Construct a Facebook URL 
$json_url ='https://graph.facebook.com/'.$page_id.''; 
$json = file_get_contents($json_url); 
$json_output = json_decode($json);   

//Facebook Like count 
echo("Like Count: {$json_output->likes}");

?>

Leave a comment

Your email address will not be published. Required fields are marked *