我正在进行curl调用(使用PHP),REST API,并且我使用以下curl选项请求标题和正文:
CURLOPT_HEADER => TRUE,
CURLOPT_NOBODY => FALSE
电话会议成功,我收到以下回复:
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Mon, 10 Apr 2017 18:42:05 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 65
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Content-Type
X-Rate-Limit-Limit: 1000
X-Rate-Limit-Remaining: 997
X-Rate-Reset: 81505
ETag: W/"41-ltrGtAoAWpkb+p1Y+yju9w"
{"name":"jim","gender":"male","probability":0.99,"count":1805}
我感兴趣的是:
X-Rate-Limit-Remaining: 997
和JSON:
{"name":"jim","gender":"male","probability":0.99,"count":1805}
这必须适用于Windows系统和Linux系统。我尝试将输出保存到$ string,然后使用:
explode ("\r\n", $string);
和
explode ("\n", $string);
但是,它不起作用。我可以单独获取标题和操作结果,但这是2个API调用,而且我的速率有限。
有什么想法吗?
答案 0 :(得分:3)
试试这个:
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$headers = explode("\n", $header);