dansan
dansan

Reputation: 249

Facebook Graph API - Access token for page likes, name and url

I'm trying to update some old PHP code which is supposed to get the name, likes and a link to the given Facebook page. This is the current old code:

$fbsite1  = json_decode(file_get_contents('http://graph.facebook.com/page1'));
$fbsite2  = json_decode(file_get_contents('http://graph.facebook.com/page2'));
$fbsite3  = json_decode(file_get_contents('https://graph.facebook.com/page3'));


for($j=1; $j<=3; $j++){
    $data[] = array(
        'name' => ${'fbsite'.$j}->name, 
        'likes' => ${'fbsite'.$j}->likes, 
        'link' => ${'fbsite'.$j}->link
    );
}

The problem is that this method requires an access token, which I'm not quite sure how to get. I've looked at the Facebook API reference, but there seems to be a few different access tokens (different permission etc.). Which one do I need to accomplish this? How do I get it?

Upvotes: 0

Views: 1920

Answers (1)

C3roe
C3roe

Reputation: 96306

If the pages are public, then you only need an app access token.

So create an app in the FB dev section (if you have not done so already), and then include your app access token in those request URLs:

http://graph.facebook.com/page1?access_token=…

FYI, you should consider requesting all that data in one go, instead of making multiple API requests. You can do that using this syntax (for up to 50 ids in one request),

http://graph.facebook.com/?ids=page1,page2,page3&access_token=…

And you will have to explicitly ask for the name, link and likes fields now (otherwise you will get name and id only):

http://graph.facebook.com/?ids=page1,page2,page3&fields=name,likes,link&access_token=…

Upvotes: 2

Related Questions