用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/