當PHP cURL 時,如果POST的數據部分超過了1024字節,發送POST請求時header部分會包含一個參數Expect:100-continue,詢問服務器是否願意接收數據,收到服務器返回的100-continue應答後再把數據POST給服務器。
在項目中遇到一個問題如下:
當傳輸的數據較少時,cURL拿到的返回是這樣的
array(2) {
["header"]=>
string(249) "HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 05:24:27 GMT
Content-Type: application/json
Content-Length: 34
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: checkinfo=; expires=Tue, 31 Mar 2015 05:24:27 GMT; Path=/
Server: TornadoServer/4.2"
["body"]=>
string(30) "{"error_msg":"","is_succ":"1"}"
}
但是數據多後,拿到的返回是這樣的array(2) {
["header"]=>
string(21) "HTTP/1.1 100 Continue"
["body"]=>
string(249) "HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 05:26:01 GMT
Content-Type: application/json
Content-Length: 34
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: checkinfo=; expires=Tue, 31 Mar 2015 05:26:01 GMT; Path=/
Server: TornadoServer/4.2"
}
正常情況下,應該返回200 OK,但是數據過的情況下header是100 Continue,body中則爲本應出現在header中的內容,接口對接方那邊說數據成功收到了,說明POST成功了,但是"{"error_msg":"","is_succ":"1"}"這個理應出現的返回沒了。爲了解決這個問題,上網查資料,發現在header中設置Expect爲空即可。
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
這樣返回信息就正常了。