niko619
niko619

Reputation: 453

Getting just number of facebook likes

I'm curious to use the facebook graph API as I don't like the simple like button with number of likes next to it. However I'm finding their documentation a bit confusing. I want to do this via PHP so I'm assuming I need the PHP SDK, is that right?

I'm looking at this page Graph API: Page however I don't understand how I would call the likes.

I'm not asking for someone to code this for me, just a little help in the right direction to understand how it works :)

Upvotes: 1

Views: 164

Answers (2)

shruti
shruti

Reputation: 780

Try this:

  $json = @file_get_contents("http://graph.facebook.com/yourpage");
  $json1 = json_decode($json,true);
  $likes = $json1['likes'];
  echo $likes;

Upvotes: 4

chrisboustead
chrisboustead

Reputation: 1563

This is pretty straightforward. Using the ID or the username of the page, access that node via the Graph API:

PAGE_ID?fields=likes

In PHP would be something like:

$token_string = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&client_secret='.$app_secret.'&grant_type=client_credentials');
$likes_response = file_get_contents('https://graph.facebook.com/PAGE_ID?fields=likes&'.$token_string);
$likes_obj = json_decode($likes_response);
$likes = $likes_obj->likes;
echo 'Likes count for page: ' . $likes;

Upvotes: 0

Related Questions