循环通过fb API朋友得到错误 [英] loop through fb API friends got error
本文介绍了循环通过fb API朋友得到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用fb graph api 1.0获取用户的朋友。
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;
}
}
我想回覆一个fb的所有朋友用户,但我得到尝试获取非对象的属性
错误。这很奇怪!
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!
这里是如何做,如果我做 echo json_encode(user_friends);
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"
}
}
这里是 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)
推荐答案
Facebook API返回一个关联数组,而不是一个对象。所以你应该这样做:
The Facebook API is returning an associative array, not an object. So you should do:
foreach($user_friends['data'] as $friend) {
echo $friend['name'];
}
这篇关于循环通过fb API朋友得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文