我有一个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];
时,我的页面上没有任何回应。所以我真的不知道我做错了什么!
有人可以就此问题提出建议吗?
任何帮助将不胜感激。
答案 0 :(得分:5)
您作为回复获得的是JSON字符串。您可以使用json_decode将其解析为stdClass
对象:
$object = json_decode( $output );
然后您可以访问所需的字段:
$total_results = $object->total_results;
或者,您可以将JSON字符串解析为数组:
$array = json_decode( $output, true );
并获取var:
$total_results = $array['total_results'];