2012-04-25 13 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ように、第2パラメータassocを追加します:次に、あなたはそうのようなあなたの$ _SESSION変数を設定することができ

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

これは、オブジェクトではなく配列を返します。次に、オブジェクト演算子ではなく配列表記[]を使用できます->

関連する問題