こんにちは皆私はPHPの初心者です、これはPHP上で初めてのアプリです。このデータにアクセスするには?
グラフのapiで新しいアルバムを作成し、写真をアップロードしようとしています。
私は、次のコードを使用しています。問題は、アルバムのFacebookグラフを作成した後で、グラフのapiがアルバムのIDを含むデータを返し、そのIDが後でそのアルバムに写真をアップロードするときです。
私の場合はデータを取得していますが、それを読み取ることができません。配列としてオブジェクトとしてアクセスしようとしましたが、何も動作していません。
私はそれを全体的に印刷しようとしたときの生存率を確認するために、このような出力を出しています。 Result: {"data":[{"id":"321215475937088","from":{"name":"Lorem …
このid要素にどのようにアクセスできますか? $ resultが配列、オブジェクト、または何かであるかどうか。私は思いつくことができるすべての可能性を試しましたが、必要な出力が得られません。
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?" . "access_token=" . $access_token;
$postdata = http_build_query(array('name' => $album_name, 'message' => $album_description));
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
//$result = json_decode(file_get_contents($graph_url, false, $context));
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo "<pre>";
echo "Result: " . $result; //output of this line is given above
echo "Result[id]: " . $result[id]; //Notice: Use of undefined constant id - assumed 'id' ...
echo "Result[data][id]: " . $result[data][id]; //Notice: Use of undefined constant data - assumed 'data'...
//Notice: Use of undefined constant id - assumed 'id'...
//Fatal error: Cannot use string offset as an array in ...
echo "Result ID: " . $result -> id;
echo "Data - >ID: " . $data->id;
echo "Data ID: " . $data[id];
echo "</pre>";
// Get the new album ID
$album_id = $result -> id;
取得しているデータはJSONです。 –
おそらく 'json_decode'はこの状況で便利でしょうか? http://php.net/manual/en/function.json-decode.php – ianace