用Curl類調用遠程接口時發現無返回結果,本地瀏覽器調用正常,懷疑是服務器網絡原因,執行命令
curl 'http://xxxx.com/?action=abc'
發現可以獲取到正常返回值,非常詭異,這下不解了,查資料后發現,原來php curl使用的liburl在發送數據時,如果數據量大於1KB時,會先發送包含header Expect:100-continue的請求與遠程服務器協商,得到能夠繼續接收數據的確認后才開始發送真正數據,如果遠程服務器沒有正確返回,則此次請求失敗,當然也就獲取不到返回結果了。
RFC關於Expect:100-continue的說明:http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.
Expect:100-continue 在HTTP/1.1中得到支持
綜上,解決方法有兩個:
1.禁用Expect
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
2.使用HTTP/1.0
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
原文地址:https://aiddroid.com/solution-for-php-curl-empty-reply-from-server/
