2017-04-10 32 views
0

我想提出一個捲曲的呼叫(使用PHP),一個REST API捲曲調用,我請求都標題和正文,使用下列捲曲選項:分離體接頭下面的PHP

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系統上工作。我試圖保存輸出到$字符串,然後使用:

explode ("\r\n", $string); 

explode ("\n", $string); 

但是,它不工作。我可以分別獲取標題和運算結果,但是這是2個API調用,而且我的速度有限。

任何想法?

回答

1

試試這個:

$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); 
+0

是不是SUBSTR($響應,0,$ header_size)一樣SUBSTR($響應,$ header_size) – EastsideDeveloper

+0

沒有,這不就是一樣的。 substr($ response,0,$ header_size)從0返回到標題中字符數的位置。 substr($ response,$ header_size)從字符後的字符返回字符串的結尾 – quentino

+0

是的,這是有效的。儘管如此,我對此有不同的看法。我已經在我的數據結構的信息部分有header_size了 – EastsideDeveloper