Ricardo Ricardo - 1 year ago 89
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

the needed Array.


// 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
// Closing

$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 (
) and it didn't help.

Answer Source

The JSON for above URL is as follows:


So, you need to adjust your grip to this:


This will output


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

$user = $results['items'][0]['user'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download