V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
floraX
V2EX  ›  问与答

PHP curl 超时问题探讨 Operation timed out after 1001 milliseconds with 0 out of -1 bytes received

  •  
  •   floraX · 2020-05-14 13:31:53 +08:00 · 2508 次点击
    这是一个创建于 1452 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我用 curl post json 数据给另外一个接口,对方可以收到数据,但是我收不到返回值。

    打印错误,Operation timed out after 1001 milliseconds with 0 out of -1 bytes received
    搜索解决办法,每有一个是对的,都是模棱两可的乱七八糟的答案,瞎扯淡!

    另外,我 post 的数据,是由 array json_encode 的 json 数据,做了一个测试,当我把 array 里面的数值,减少到 2 -3 个时候,就有返回值了,但是 array 里面的也就 20 -30 个数值,也不大啊,

    头疼,有朋友预计过不,探讨下 !


    代码如下:

    $curl = curl_init();
    curl_setopt_array($curl,
    array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "gzip",
    //CURLOPT_MAXREDIRS => 200,
    CURLOPT_TIMEOUT => 1,
    //CURLOPT_NOSIGNAL=>1,
    //CURLOPT_TIMEOUT_MS=>200,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => array("Content-Type: application/json; charset=utf-8"),
    )
    );
    $response = curl_exec($curl);
    //$err = curl_error($curl);
    curl_close($curl);
    4 条回复    2020-05-15 10:19:42 +08:00
    littleylv
        1
    littleylv  
       2020-05-14 13:37:40 +08:00
    CURLOPT_TIMEOUT => 1,

    https://www.php.net/manual/en/function.curl-setopt.php :
    CURLOPT_TIMEOUT The maximum number of seconds to allow cURL functions to execute.

    你逗我的吗
    floraX
        2
    floraX  
    OP
       2020-05-14 13:48:58 +08:00
    @littleylv 感谢 V 友的回复,我把 CURLOPT_TIMEOUT 改为 10,依然没有返回值,error 还是 Operation timed out after 10001 milliseconds with 0 out of -1 bytes received


    谢谢了 !
    msg7086
        3
    msg7086  
       2020-05-14 19:34:59 +08:00 via Android
    超时啊,服务器端还没返回数据你就傲娇关连接了。
    对方收到数据以后及时返回了吗?
    floraX
        4
    floraX  
    OP
       2020-05-15 10:19:42 +08:00
    @msg7086 对方确实收到收据了,也返回了,我这边就是 报超时 的错误。

    当我把 post 的数据内容减少,就可以了。初步怀疑,是 post 量大,但的确没多少,是不是应该 改啥配置文件啥的。
    另外,有 上传文件接口,50m,没问题。

    感觉是 curl 的问题,post 量多,就没有返回值,报超时错误,量少,就正常收到返回值,不清楚修改啥配置呢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   769 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:25 · PVG 04:25 · LAX 13:25 · JFK 16:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.