Link to home
Start Free TrialLog in
Avatar of qvfps
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.  

<?php
$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);
curl_close($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";
}

?>

{"items":[{"TicketID":41,"WorkHoursID":53,"StartWorkHour":"2019-09-16T13:33:11Z","EndWorkHour":"2019-09-16T13:33:13Z","TechnicianContactID":2,"Billiable":true,"OnCustomerSite":false,"Description":null,"TechnicianFullName":"Tech1","TechnicianEmail":"Tech1@support.com"},{"TicketID":41,"WorkHoursID":82,"StartWorkHour":"2019-08-05T15:23:21Z","EndWorkHour":"2019-08-05T15:26:33Z","TechnicianContactID":7,"Billiable":true,"OnCustomerSite":false,"Description":null,"TechnicianFullName":"Tech2","TechnicianEmail":"Tech2@support.com"}],"totalItemCount":2,"page":1,"itemsInPage":10,"totalPages":1,"prevLink":"","nextLink":""}


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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qvfps
qvfps

ASKER

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