Hello
I am trying to access test data from the following free API site : http://dummy.restapiexample.com/api/v1/employees
I am trying to do this via the “curl” command but keep getting the error “Trying to get property ‘employee_name’ of non-object”
When I use the “file_get_contents” method though it works fine
Below is the 2 functions are am trying to use. “callapi” uses curl and does not work. “callapi2” uses the “get_file_contents” method and does work
What am I doing wrong with the curl command?
<?php
function callapi($url,$fieldname) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
$data = json_decode($result);
echo 'employee name : ' . $data[0]->{'employee_name'};
curl_close($ch);
return $data;
}
function callapi2($url,$fieldname) {
$contdata = file_get_contents($url);
echo $contdata;
$jsonobj = json_decode($contdata);
echo 'employee name : ' . $jsonobj[0]->{'employee_name'};
return $jsonobj;
}
?>
Thanks
Mark