Facebook like counter

Using the facebook graph api you can get facebook like count about any particular id or url. You can use facebook ID, friendly url or any url, for example:

  • Facebook ID: 6358087478
  • Facebook friendly url: php
  • Url: http://php.net/

Get facebook like count using graph api

function facebook_likes($url){
    $data = file_get_contents("http://graph.facebook.com/?ids=" . $url);
    $data = json_decode($data, true);
    return $data;
}

$response = facebook_likes('php');
print_r($response);

Output:

Array
(
    [php] => Array
        (
            [talking_about_count] => 3259
            [website] => www.php.net
            [were_here_count] => 0
            [id] => 6358087478
            [name] => PHP
            [link] => http://www.facebook.com/PHP
            [likes] => 377124
        )

)

With the graph api, you can also retrieve likes count for more than one url/id using comma as id separator.

function facebook_likes($urls){
    $urls = implode(",", $urls);
    //http://graph.facebook.com/?ids=php,mysql
    $data = file_get_contents("http://graph.facebook.com/?ids=" . $urls);
    $data = json_decode($data, true);
    return $data;
}

$urls = array('php', 'mysql');
$response = facebook_likes($urls);
print_r($response);

Output:

Array
(
    [php] => Array
        (
            [talking_about_count] => 3259
            [username] => PHP
            [were_here_count] => 0
            [id] => 6358087478
            [name] => PHP
            [link] => http://www.facebook.com/PHP
            [likes] => 377236
        )

    [mysql] => Array
        (
            [talking_about_count] => 2252
            [username] => mysql
            [were_here_count] => 0
            [id] => 7802084642
            [name] => MySQL
            [link] => http://www.facebook.com/mysql
            [likes] => 200791
        )

)

Get facebook like count using fql

function facebook_shares($url){
    $query  = " SELECT url, normalized_url, share_count, like_count, ";
    $query .= " comment_count, total_count, comments_fbid, click_count ";
    $query .= " FROM link_stat WHERE url = '".$url."'";

    $api_url ="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($query);
    $response = file_get_contents($api_url);
    return json_decode($response, true);
}

$response = facebook_likes("http://php.net/");
print_r($response);

Output:

Array
(
    [0] => Array
        (
            [url] => http://php.net/
            [normalized_url] => http://www.php.net/
            [share_count] => 2698
            [like_count] => 2343
            [comment_count] => 783
            [total_count] => 5824
            [comments_fbid] => 10150109266823062
            [click_count] => 1094
        )

)

To retrieve likes for more than one id or url you can use the following query:

function facebook_likes($urls){
    $urls = "'" . implode("','", $urls) . "'";
    $query  = " SELECT url, normalized_url, share_count, like_count, ";
    $query .= " comment_count, total_count, comments_fbid, click_count ";
    $query .= " FROM link_stat WHERE url IN($urls)";

    $api_url ="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($query);
    $response = file_get_contents($api_url);
    return json_decode($response, true);
}

$urls = array('http://php.net', 'http://apache.org/');
$response = facebook_shares($urls);
print_r($response);

Output:

Array
(
    [0] => Array
        (
            [url] => http://php.net
            [normalized_url] => http://www.php.net/
            [share_count] => 2698
            [like_count] => 2344
            [comment_count] => 783
            [total_count] => 5825
            [comments_fbid] => 10150109266823062
            [click_count] => 1094
        )

    [1] => Array
        (
            [url] => http://apache.org/
            [normalized_url] => http://www.apache.org/
            [share_count] => 461
            [like_count] => 92
            [comment_count] => 86
            [total_count] => 639
            [comments_fbid] => 10150152124326836
            [click_count] => 72
        )

)

See also: Get the number of google plus shares

- Posted by Eva to Php