2017-08-10 50 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分區,我試圖恢復他們,但沒有什麼: ()