Hallo an alle, ich bin ein Neuling zu PHP und das ist meine erste App über PHP.Wie kann auf diese Daten zugegriffen werden?
Ich versuche, ein neues Album durch Graph API zu erstellen und Fotos hochladen.
Ich habe den folgenden Code an Ort und Stelle, Problem ist, wenn nach dem Erstellen eines Albums Facebook Graph API Daten gibt, die die ID des Albums enthält und diese ID später zum Hochladen von Fotos in das Album verwendet wird.
In meinem Fall bekomme ich die Daten, aber ich bin nicht in der Lage, durch sie zu lesen, ich habe versucht, es als ein Objekt, als Array, aber keiner funktioniert.
Um die Lebensfähigkeit zu überprüfen, wenn ich versuchte, es ganz zu drucken, gibt es Ausgabe so. Result: {"data":[{"id":"321215475937088","from":{"name":"Lorem …
Ich möchte wissen, wie ich auf dieses ID-Element zugreifen kann? ob $ result ein Array, ein Objekt oder was ist. Ich habe jede Möglichkeit ausprobiert, die mir einfiel, bekomme aber nicht die erforderliche Leistung.
// 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;
Die Daten, die Sie erhalten, sind JSON. –
wahrscheinlich 'json_decode' kommt in diesen Situationen praktisch? http://php.net/manual/en/function.json-decode.php – ianace