Ricardo Ricardo - 2 months ago 13
JSON Question

How to properly extract an Array from a cURL JSON?

I'm trying to extract a user profile picture from Instagram but I can't manage to successfully

echo
the needed Array.

$url="https://www.instagram.com/selenagomez/media/";

// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$json=curl_exec($ch);
// Closing
curl_close($ch);

$result = json_decode($json, true);

echo $result[0]["profile-picture"];


I'm getting:
Notice: Undefined offset: 0 in test.php on line 23


Also tried to exclude the 2nd JSON parameter (
true
) and it didn't help.

Answer

The JSON for above URL is as follows:

Source

So, you need to adjust your grip to this:

$results['items'][0]['user']['profile_picture'];

This will output

https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xfl1/t51.2885-19/s150x150/12918537_1719366751611008_1708400518_a.jpg

Go back one level in the tree to get user property with all its children

$user = $results['items'][0]['user'];