user2388347
user2388347

Reputation: 23

How to get number of Facebook likes?

I am trying to get the number of Facebook likes on my Facebook page. Currently, I am able to get it using this:

function fblikes() {
$pageID = $_GET['id'];
$pagelikes = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
echo $pagelikes->likes;
}

Except, I would like it to display the short form of the number of likes instead of the entire number.

For example: When I get the number of likes from http://graph.facebook.com/facebook, it displays as "91830595". I would like the number of likes to display as it does on http://www.facebook.com/facebook - "91m".

Some help would be appreciated.

Upvotes: 2

Views: 1826

Answers (2)

Dampas
Dampas

Reputation: 323

The Yogesh answer is good, but the question has error.

For code to work you need to put

echo $pagelikes[ 'likes' ];

instead line

echo $pagelikes->likes;

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Don't know from Facebook api we can do this, but you can do this in PHP

function nice_number($n) {
    // first strip any formatting;
    $n = (0+str_replace(",","",$n));

    // is this a number?
    if(!is_numeric($n)) return false;

    // now filter it;
    if($n>1000000000000) return round(($n/1000000000000),2).' trillion';
    else if($n>1000000000) return round(($n/1000000000),2).' billion';
    else if($n>1000000) return round(($n/1000000),2).' million';
    else if($n>1000) return round(($n/1000),2).' thousand';

    return number_format($n);
}

echo nice_number($pagelikes->likes);  // 91million

Upvotes: 4

Related Questions