【问题标题】:Why is cURL hanging after sending and receiving an HTTP 100 Continue code, not continuing with the body?为什么 cURL 在发送和接收 HTTP 100 Continue 代码后挂起,而不是继续正文?
【发布时间】:2017-08-10 20:23:52
【问题描述】:

我目前正在整理一些数据并向我的本地 Web 服务器 (Apache 2.4.27) 发出 POST 请求。

以下是我整理请求的方式:

public function sendPost($url, array $data) {
    $ch = curl_init($url);
    $httpCodes = parse_ini_file(CONF::HTTP_CODES_INI);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 30);
    curl_setopt($ch, CURLOPT_TIMEOUT , 30);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => json_encode($data)]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    //Ignore SSL issues, testing on a dev box
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $curlInfo = curl_getinfo($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    $curlInfo["http_code"] = $httpCode . ': ' . $httpCodes[$httpCode];
    if ($result === false || $httpCode > 299) {
        $result = json_encode($curlInfo);
    }
    curl_close($ch);
    return $result;
}

这是我从 cURL 看到的对话

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to my.local.server.com (127.0.0.1) port 80 (#0)
> POST /path/script.php?action=receive HTTP/1.1
Host: my.local.server.com
Accept: */*
Content-Length: 2645588
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------051580b0f5143de8

< HTTP/1.1 100 Continue
* Operation timed out after 30000 milliseconds with 0 bytes received
* Curl_http_done: called premature == 1
* Closing connection 0

但是,在服务器上调试脚本时,我确实获得了数据,并且请求似乎一直通过。根据我对HTTP 100 Continue状态码的理解,cURL应该只先发送header,看到continue的响应,再发送数据。

您可以在上面看到客户端收到HTTP/1.1 100 Continue,但随后超时。

我尝试将超时时间设置为 5 分钟,但它仍然会挂起直到超时。

有什么想法吗?

编辑:我尝试过使用

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

尝试强制不设置 continue 期望。响应是一样的,只是没有Expect: ... 部分。

【问题讨论】:

  • 它说“0 bytes received”,这可能是关键。它很可能已经发送了所有字节,但它没有收到任何东西......(嗯,在“100 continue”之后)

标签: php curl


【解决方案1】:

得到了一些理论,1:可能是 curl 中的一个错误。 2:可能是服务器中的一个错误,服务器停止读取,并且接收缓冲区已满,curl 等待服务器读取,这永远不会发生,它会超时...... 3:也许你的连接速度很慢,发送 2645588 字节确实需要 5 分钟以上。

您应该添加一个 CURLOPT_PROGRESSFUNCTION 来检查传输速度,这有望排除或确认理论 2 和 3,例如

$starttime = microtime ( true );
curl_setopt_array ( $ch, array (
        CURLOPT_NOPROGRESS => false,
        CURLOPT_PROGRESSFUNCTION => function ($ch, int $expectedDownloadBytes, int $bytesDownloaded, int $expectedUploadBytes, int $bytesUploaded) use (&$starttime): int {
            var_dump ( 'runtime seconds:', microtime ( true ) - $starttime, 'uploaded so far:', $bytesUploaded, 'expected to upload:', $expectedUploadBytes );
            return 0;
        } 
) );
  • 如果这没有产生任何线索,您可以使用 socket_create & co 手动创建一个 POST 请求,但我现在没有示例可以向您展示(由于有时意外删除了一些 misc 脚本文件夹回来,在压缩的 btrfs 分区上,我尝试恢复 em,但没有:()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 2013-01-07
    • 2015-09-30
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多