Getting friends list using facebook api's

2019-04-14 22:41发布

问题:

I have the following command which will allow me to post a message on the logged in users facebook wall:

$facebook->api("/$uid/feed", "POST",  array('message' => 'Hello! I\'m using the FB Graph API!'));

i.e. using the url https://graph.facebook.com/<user id goes here>/feed

But what command do I need to enter to get a list of the logged in users friends?

The url I need to use is https://graph.facebook.com/me/friends, but I am not sure how to edit the below command to get the friends list:

$facebook->api("/$uid/feed", "POST",  array('message' => 'Hello! I\'m using the FB Graph API!'));

am I supposed to do something like this (which is a complete guess)

$facebook->api("/$uid/friends", "GET",  array('access_token' => 'token value goes here'));

Or am I supposed to do something else to get the friends list?

回答1:

If you have a valid session, then this should retrieve the current user's friends:

$friends = $facebook->api("/me/friends")

But if you don't have a valid session (or need to retrieve this offline) you need the offline_access permission and your code will also work.
UPDATE:
Since the deprication of the offline_access permission, you need a long-lived access_token.


Please note that you can get the friends list with this FQL query too:

$friends = $facebook->api(array(
    "method"    => "fql.query",
    "query"     => "SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
));


回答2:

This post on SO may provide the answer you are looking for. You will need to get the friends object then do a foreach loop over it to print the results (as described in the post). Read Getting list of Facebook friends with latest API