Retrieve Facebook Likes Count with PHP

You can get the total number of likes and share count of any web URL with a little bit of PHP and FQL (Facebook Query Language).

You don’t have to fiddle with the Facebook SDK or the API and this trick works for both Facebook pages as well as any external web page that has the like button or has been shared on Facebook.


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);

}

$fb = facebook_count('https://www.facebook.com/digital.inspiration');

// facebook share count
echo $fb[0]->share_count;

// facebook like count
echo $fb[0]->like_count;

// facebook comment count
echo $fb[0]->comment_count;