Rob M
Rob M

Reputation: 121

Facebook Graph api doesn't return everything

I just found out that Facebook updated their graph api and now require an access token to retrieve like counts for a page to be displayed on an external website. I went through the motions of setting up an app to get the token and have the following:

    $fbData = json_decode(file_get_contents('https://graph.facebook.com/myFacebookName?access_toke=MyAppId|MySecretToken'));
                print_r($fbData);

It seems to only be returning objects:

 stdClass Object ( [name] => My Facebook Name [id] => id number )

And that is all, giving me nothing to parse through. The name and id coming through are correct so there is a connection happening. Note that I have edited out some information so MyFacebookName and MyAppId|MySecretToken are actually populated with the correct info. Any ideas how to get the full JSON list to grab page likes? Am I missing something?

UPDATE Thanks to Tobi and further reading the graph api, documentation I was able to get the number of likes with with following:

$fbData = json_decode(file_get_contents('https://graph.facebook.com/name?fields=likes&access_token=appId|accessToken'));
<?php echo number_format($fbData->likes);?>

Upvotes: 0

Views: 279

Answers (1)

Tobi
Tobi

Reputation: 31489

You need to specifically request the fields you want in the response. In your case this would be

https://graph.facebook.com/myFacebookName?fields=id,name,likes&access_toke=MyAppId|MySecretToken

I don't understand why you say there "nothing to parse", because the object IS returned and can be used. So what's the problem?

Using echo $fbData['likes'] should give you the number of likes.

See

Upvotes: 2

Related Questions