2017-08-10 52 views
0

我目前正在组织一些数据并向我的本地Web服务器(Apache 2.4.27)发出POST请求。为什么在发送和接收HTTP 100继续代码后cURL挂起,而不是继续执行正文?

这里是我如何放在一起的要求:

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; 
} 

这是我从卷曲

* 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应该首先发送标题,请参阅continue响应,然后发送数据。

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

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

任何想法?

编辑:I'ved试图用

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

,试图迫使continue期望,不能设置。回应是一样的,只是没有Expect: ...部分。

+0

它说“0字节** **收到**”,这可能是关键。它可能已经发送了所有字节,但它没有收到任何东西......(好吧,在“100继续”之后) –

回答

1

得到了一些理论,1:也许卷曲的错误。 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; 
     } 
)); 
  • 如果不产生任何线索,你可以创建一个POST请求手动与socket_create &合作,但我没有一个例子来显示你现在(由于意外删除了一些misc脚本文件夹在压缩btrfs分区,我试图恢复他们,但没有什么: ()