我正在尝试了解如何在服务器之间传输数据。在线有一个包含 json 数据的测试 API。我尝试了以下操作:-
<?php
// Initiate curl session in a variable (resource) $curl_handle = curl_init();
$url = "http://dummy.restapiexample.com/api/v1/employees"; // website sample API data
// Set the curl URL option curl_setopt($curl_handle, CURLOPT_URL, $url);
// This option will return data as a string instead of direct output curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// Execute curl & store data in a variable $curl_data = curl_exec($curl_handle);
curl_close($curl_handle);
// Write the JSON string
// echo $curl_data; // the above writes the JSON string ok
// Now try decoding to PHP array
$character = json_decode($curl_data);
echo $character[1]->employee_name;
// this throws an error 'Error: Cannot use object of type stdClass as array in C:\wamp64\www\curlex\curlget.php on line 24'
?>
返回的字符串包含以下内容(为清楚起见,已删除为 2 个条目):-
{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},{"id":"2","employee_name":"Garrett Winters","employee_salary":"170750","employee_age":"63","profile_image":""}]}
我想 json_decode 由于{"status":"success","data":序言而失败?请问如何解决?
动漫人物
郎朗坤
幕布斯6054654