user3581183
user3581183

Reputation: 39

Facebook php-sdk v4 get friend list

i'm using new facebook php sdk v4 and I want to retrieve full list of friends Code:

try {

$user_friends = (new FacebookRequest(
  $session, 'GET', '/me/friends'
))->execute()->getGraphObject(GraphUser::className());

echo '<pre>';
print_r($user_friends);

 } catch(FacebookRequestException $e) {

echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();

} 

But the result is:

Facebook\GraphUser Object
 (
[backingData:protected] => Array
    (
    )
  )

Login url parameters:

$loginUrl = $helper->getLoginUrl(array( 'publish_actions', 'user_friends', 'public_profile' ) );

Is this feature is disabled? There is another way to get a list of friends?

Thank you in advance

Upvotes: 2

Views: 10127

Answers (2)

Somnath Muluk
Somnath Muluk

Reputation: 57846

You will get only those friends who are using same Facebook application.

\Facebook\FacebookSession::setDefaultApplication('id','secret');

$session = new \Facebook\FacebookSession('access_token');

// Check user's token is valid or not.
$me = (new \Facebook\FacebookRequest(
$session, 'GET', '/me/friends'
))->execute()->getGraphObject(\Facebook\GraphUser::className());

$result = $me->asArray();

// Get user's friends
$friends = $result['data'];

// Converting classes to array
foreach ($friends as $key => $value) {
    $friends[$key] = (array)$value;
}

Upvotes: 3

Tobi
Tobi

Reputation: 31489

Have a look at my answer here retrieve full list of friends using facebook API

The edge /{user-id}/friends only return friends which have used the same app as the requesting user: https://developers.facebook.com/docs/graph-api/reference/v2.0/user/friends/

Please make sure that you use the search functionality of SO before you post a question next time, because there were already lots of question regarding the same topic.

Upvotes: 0

Related Questions