Arrays 为在Laravel 4中解压缩Facebook数组的foreach()提供的参数无效
我试图将Facebook用户朋友的FBID存储在mysql数据库的列中。我已经试着寻找关于这个问题的其他答案,并且我已经试着实现它(在Laravel4中)。以下是我所做的: 在Facebook.php文件中,提供程序之一:Arrays 为在Laravel 4中解压缩Facebook数组的foreach()提供的参数无效,arrays,facebook,laravel,laravel-4,Arrays,Facebook,Laravel,Laravel 4,我试图将Facebook用户朋友的FBID存储在mysql数据库的列中。我已经试着寻找关于这个问题的其他答案,并且我已经试着实现它(在Laravel4中)。以下是我所做的: 在Facebook.php文件中,提供程序之一: 'friends' => 'https://graph.facebook.com/me/friends?access_token='.$token->access_token 在我的Oauth2控制器中: $friends_list = $user['fri
'friends' => 'https://graph.facebook.com/me/friends?access_token='.$token->access_token
在我的Oauth2控制器中:
$friends_list = $user['friends'];
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
然后我想将$friend_id对象存储在数据库中的“text”列中。但是,在运行此命令时,我不断收到错误:为foreach()提供的参数无效。
但很明显,它被提供了一个数组。有什么我没看见的吗?谢谢你的帮助 实际上返回的结果是一个
json
,返回的对象应该如下所示
{
"id": "xxxxxxx",
"name": "Sheikh Heera",
"friends": {
"data": [
{ "name": "RaseL KhaN", "id": "xxx" },
{ "name": "Yizel Herrera", "id": "xxx" }
],
"paging": {
"next": "https://graph.facebook.com/xxx/friends?limit=..."
}
}
}
在您json\u解码后
$user = json_decode($user, true);
它应该看起来像
Array
(
[id] => xxxxxxx
[name] => Sheikh Heera
[friends] => Array
(
[data] => Array
(
[0] => Array
(
[name] => RaseL KhaN
[id] => xxx
)
[1] => Array
(
[name] => Yizel Herrera
[id] => xxx
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/xxx/friends?limit=...
)
)
)
所以,现在你可以
$friends_list = $user['friends'];
$data = $friends_list['data'];
确保$data
数组不为空,然后循环
if(count($data)) {
$friend_ids_arr = array();
foreach($data as $friend) {
$friend_ids_arr[] = $friend['id'];
}
}
因此,只有当$data
中有项目时,foreach
才会运行
更新:它可能会帮助您
$url = "https://graph.facebook.com/me?fields=id,name,friends&access_token=YOUR_ACCESS_TOKEN";
$contents = json_decode(file_get_contents($url), true);
$friends = $contents['friends'];
$friend_ids_arr[]
foreach($friends['data'] as $friend)
{
$friend_ids_arr[] = $friend['id'];
}
然后确保
$user['friends']
不为空,尝试var\u dump($user['friends'])
。它肯定不是空的,只是返回一个url。因此,如果我使用关联的访问令牌访问图形url,它会列出所有内容。如何从图ulr中获取数据部分?它仍然表示:引用$data=$friends_list['data']的非法字符串偏移量'data';Line您可以var\u dump()
返回的结果吗?它只是在页面上显示了一个错误,即llegal字符串偏移量“data”