Hello everyone I am a newbie to php and this is my first ever app over php.
I am trying to create a new album through graph api and upload photos to it.
I’ve the following code in place, problem is when after creating an album facebook graph api returns data that contains the id of the album and that id is later used to upload photos to that album.
In my case I am getting the data but I am unable to read through it, I tried to access it as an object, as an array but none is working.
To check the viability when I tried to print it whole it is giving output like this.
Result: {"data":[{"id":"321215475937088","from":{"name":"Lorem …
I want to know how I can access this id element? whether $result is an array, object or what. I’ve tried every possibility that I could come up with but am not getting the required output.
// 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;
You’re getting JSON serialized object.
You need to deserialize it before use.
You could do it like this:
After that you can access properties of the object:
*Please note that data in your response string is an array which contains objects