2013-07-06 15 views
1

我想的Facebook用户的朋友的FBIDs存储在MySQL数据库中的列。我试着在这个问题上寻找其他答案,并试图实现它(在Laravel 4中)。这是我做了什么:无效的参数()拆包Facebook的数组中Laravel 4

在Facebook.php文件,供应商之一:

'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_ids对象在我的数据库的“文本”列中。然而,运行这个时候,我不断收到错误:Invalid argument supplied for foreach()

但它很清楚被提供一个数组作为它应该。有没有我没有看到的东西?感谢您的帮助。

回答

0

实际上返回的结果是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_decode

$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']; 
    } 
} 

所以,foreach将仅运行时$data中有项目。

更新:它可以帮助你

$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']; 
} 
+0

它说的错误: 非法串偏移 '数据' – user1072337

+0

然后确保'$用户[ '朋友']'不为空,尝试'后续代码var_dump($用户[ '朋友'])'。 –

+0

这绝对不是空的,它只是返回一个网址。因此,如果我使用关联的访问令牌转到图表url,它会列出整个事件。我如何才能从图形ulr中获取数据部分? – user1072337