Link to home
Create AccountLog in
Avatar of qvfps

asked on

PHP Curl Json issues

I am trying to write a small application in PHP do download some content using curl in json format and process it.     Below is the data returned via curl and the error message I get when trying to walk through it.   I am sure it is something small I have done wrong I just cant see it.  

I appreciate any suggestions.  

$url = "https://website/records";
$apikey = '11111111111111111111111';
$ch = curl_init( $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-KEY: ' .$apikey, 'Content-Type: application/json' , 'Accept: application/json'));
$myout = curl_exec($ch);

//$result = json_decode(json_decode($myout,true));
$result = json_decode($myout);
echo $myout;
$myResult = $result;

foreach($myResult as $value){
       echo $value->name . ",  " , $value->gender . "<br>\n";



Invalid argument supplied for foreach()
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of qvfps


This is my first time trying to use curl and json so it was just me being an idiot and copying an example verbatim and wondering why it didn't work with different input.     I should have looked at the sample data and realized Name and Gender were items and not default properties as I was assuming for some unknown reason.      

Thanks for the help