Facebook php-sdk v4 get friend list

user3581183 picture user3581183 · May 13, 2014 · Viewed 10.1k times · Source

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

Answer

Somnath Muluk picture Somnath Muluk · Nov 20, 2014

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;
}