2012-04-25 6 views
0

사용자의 위치를 ​​디코딩하고 싶습니다. 한다고 가정Facebook 디코딩 Json

"id": "100000564553314", 
"name": "Adi Mathur", 
"location": { 
     "id": "106487939387579", 
     "name": "Gurgaon, Haryana" 
} 

내가 이름을 얻을 수있는 스크립트를 사용하고 있지만 위치는 나에게 오류

을주고있다

배열로 입력 stdClass의 객체를 사용 할 수 없음

$token_url = "https://graph.facebook.com/oauth/access_token?" 
    . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret . "&code=" . $code; 

$response = file_get_contents($token_url); 
$params = null; 
parse_str($response, $params); 

$graph_url = "https://graph.facebook.com/me?access_token=" 
. $params['access_token']; 

$user = json_decode(file_get_contents($graph_url)); 

echo $_SESSION['name']=$user->name; // WORKS 
echo $_SESSION['fbid']=$user->id;  // WORKS 

echo $_SESSION['location']=$user->location[0]; // ERROR 
echo $_SESSION['location']=$user->location->name; // ERROR 

답변

1

사용을 고려 :

$user = json_decode(file_get_contents($graph_url), true); 

이렇게하면 $ user가 객체가 아닌 연관 배열이됩니다.

$_SESSION['name']=$user['name']; 
$_SESSION['fbid']=$user['id']; 
$_SESSION['location']=$user['location']['name']; 
1

json_decodetrue로 두 번째 매개 변수 assoc 추가 : 그럼 당신은 다음처럼 $ _SESSION 변수를 설정할 수 있습니다

json_decode(file_get_contents(...),true); 

이 객체가 아닌 배열을 반환합니다. 그런 다음 객체 연산자 대신 배열 표기법 []을 사용할 수 있습니다. ->