Get Friends

Problem

I need to retrieve all of the current loggedinuser’s friends.

Solution

Use the Friends.get() method:

$friends = $facebook->api_client->friends_get();

You can optionally specify a flid (friend list ID) if you want to narrow the scope of the retrieval down to a single friend list:

$friends = $facebook->api_client->friends_get(12345);

You can find the flids for this user by calling Friends.getLists() (see Get Friend Lists).

Discussion

Friends.get() returns an array of uids for the matching friends.

This method will only return friends of the current loggedinuser. There is no way to call this for a different user. It is against the terms of use of Facebook Platform to store the returned values from this call.

FQL equivalent

If you’d prefer to use FQL to access friends, the equivalent query is:

SELECT uid2 FROM friend WHERE uid1=$uid

See Retrieving a User’s Friends for more information.

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.