我有一个CURL响应,如下所示:
{"page_number":2,"items":[],"items_per_page":1,"kind":"search#companies","total_results":0,"start_index":1}
我所需要的只是价值 "total_results":0
所以我"total_results"
想这样:
$url = "https://api.companieshouse.gov.uk/search/companies?q=POOdfgdfgyygfhfgfgfghgfP&items_per_page=1&start_index=0"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, "my_password"); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $output = curl_exec($ch); $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $info = curl_getinfo($ch); curl_close($ch); echo $output->total_results[0];
然而,当我$output->total_results[0];
回应时,我的页面上没有任何回应.所以我真的不知道我做错了什么!
有人可以就这个问题提出建议吗?
任何帮助,将不胜感激.
你得到的回应是JSON字符串.您可以stdClass
使用json_decode将其解析为对象:
$object = json_decode( $output );
然后,您可以访问所需的字段:
$total_results = $object->total_results;
或者,您可以将JSON字符串解析为数组:
$array = json_decode( $output, true );
得到var:
$total_results = $array['total_results'];