如何使php curl请求永远等待响应(在成功发送请求后)


How to make php curl request wait forever for the response (after successfully sending the request)

我试图在php脚本中使用curl将大zip文件发送到tomcat应用程序。因为它是一个大的zip文件,所以在tomcat服务器上要花一些时间来解压缩这个zip文件(大约2-5分钟),但是curl请求等待的时间不会超过30秒,然后它就会继续,就好像它收到了一个空的响应。

代码我可以重现问题:

set_time_limit(0);
$uploadURL = 'http://192.168.0.2:8080/some/url/'
$userid = 'a-user';
$password = 'a-password';
$zipfile = '/tmp/myfile.zip';
$ch = curl_init($uploadURL);
curl_setopt($ch, CURLOPT_HEADER, array(
    'Connection: Keep-Alive',
    'Keep-Alive: 3600'
    ));
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_USERPWD, $userid.":".$password);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
  "uploadMode" => "uploadOnly",
  "id" => $id,
  "numberOfFiles" => "1",
  "file"=>"@".$zipfile.";type=application/zip"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 45);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
$response = curl_exec($ch);

我唯一的怀疑是curl发出请求,然后由于没有来回发送任何字节而经过一定的秒数而超时(也称为between_bytes_timeout)。但是我找不到curl选项来帮助解决这个问题,所以我希望它是其他的。

tomcat服务器是干净的,因为我可以用我的浏览器向它发出请求,可以持续几个小时而没有问题。

很可能,cURL只是自动取消请求,因为在Tomcat解压缩时传输速率太低。

如果在CURLOPT_LOW_SPEED_TIME秒内平均传输速率低于CURLOPT_LOW_SPEED_LIMIT字节/秒,就会发生这种情况。

尝试添加带有高时间和/或低限制的适当选项,例如:

curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1);   // cancel if below 1 byte/second
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30);   // for a period of 30 seconds

为了快速测试,我建议使用比Tomcat实际需要的时间稍高的时间来解包给定的测试zip。

对于设置请求头,使用CURLOPT_HTTPHEADER而不是CURLOPT_HEADER