2

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

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
user3581183
  • 39
  • 1
  • 1
  • 4

2 Answers2

3

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;
}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
0

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.

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • Thank you, I searched but did not find this post. So I have to send an application for review to be able to retrieve friends data(like first name, last name)? This is the only option? – user3581183 May 13 '14 at 13:45
  • 1
    If you're using v2.0 of the Graph API, you cannot access friends information other than those given by `/{user-id}/friends`, `/{user-id}/taggable_friends` and `/{user-id}/invitable_friends` which are not much. Friends permissions have been removed: https://developers.facebook.com/docs/apps/changelog#v2_0_permissions – Tobi May 13 '14 at 13:51
  • Thanks but i have other problem, on index.php page if i check user's session I can see that I'm logged in, but in other.php I'm not logged in. I use this same code in both files. – user3581183 May 13 '14 at 17:19
  • That's not related to your question. – Tobi May 13 '14 at 18:36
  • Sorry, posted new question. – user3581183 May 13 '14 at 19:41