0

ユーザーの場所をデコードしたい。仮定する

"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
4

2 に答える 2

1

以下の使用を検討してください。

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

これにより、$user はオブジェクトではなく連想配列になります。次に、次のように $_SESSION 変数を設定できます。

$_SESSION['name']=$user['name'];
$_SESSION['fbid']=$user['id'];
$_SESSION['location']=$user['location']['name'];
于 2012-04-25T16:01:29.397 に答える
1

assoc次のように 2 番目のパラメータを追加しtrueますjson_decode

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

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

于 2012-04-25T16:01:31.760 に答える