zoukankan      html  css  js  c++  java
  • 诡异的php curl error Empty reply from server

    用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/
  • 相关阅读:
    《构建之法》前三章读后感--软件工程
    复利计算--web版--总结--软件工程
    利率计算v2.0--web版--软件工程
    <更新日期03-31-2016> 复利计算5.0 <已改进>
    0302随笔
    有限自动机的构造与识别
    评论
    C语言文法
    词法分析 after Coding
    词法分析
  • 原文地址:https://www.cnblogs.com/pengyong1211/p/15103974.html
Copyright © 2011-2022 走看看