Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using fb graph api 1.0 to get friends of user.

$facebook   = new Facebook(array(
        'appId' => $appid,
        'secret' => $appsecret,
        'cookie' => TRUE,
    ));
    $fbuser = $facebook->getUser();
    if ($fbuser) {
        try {
            $user_friends = $facebook->api('/me/friends');

            foreach($user_friends->data as $friend){
                    echo $friend->name;
            }
        }

I want to echo all of the friends of a fb user, but I got Trying to get property of non-object error. That's so strange!

here is how it look if I do echo json_encode(user_friends);

{
   "data":[
      {
         "name":"JIN",
         "id":"100007934492797"
      },
      {
         "name":"aris",
         "id":"100008128873664"
      },
      {
         "name":"Madm",
         "id":"34234234"
      }
   ],
   "paging":{
      "next":"https://graph.facebook.com/v1.0/1380314981/friends?limit=5000&offset=5000&__after_id=enc_AeyRMdHJrW0kW9vIZ41uFPXMPgE-VwRaHtQJz2JWyVc0hMl9eOG10C6JWjoCO8O2E4m24EPr28gIt9mxQR8oIQmN"
   }
}

Here is var_dump($user_friends):

array (size=2)
  'data' => 
    array (size=477)
      0 => 
        array (size=2)
          'name' => string 'Ajmaltert' (length=10)
          'id' => string '93524316' (length=7)
      1 => 
        array (size=2)
          'name' => string 'Morgertan' (length=13)
          'id' => string '5013347575' (length=9)
      2 => 
        array (size=2)
          'name' => string 'Mohd Muntalip' (length=22)
          'id' => string '5087024751' (length=9)
      more elements...
  'paging' => 
    array (size=1)
      'next' => string 'https://graph.facebook.com/v1.0/1380314981/friends?limit=5000&offset=5000&__after_id=enc_Aex1c8dtr7YsIz0Zn2fbJiz5XSI9FwPv9jkXfCCzRq0U7aBVGxjuJIZiKib-q3Rw99D-S6oufCTul4IFZkkgowpA' (length=177)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

The Facebook API is returning an associative array, not an object. So you should do:

foreach($user_friends['data'] as $friend) {
    echo $friend['name'];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...