【问题标题】:How to make php curl request wait forever for the response (after successfully sending the request)如何使php curl请求永远等待响应(成功发送请求后)
【发布时间】:2011-09-23 09:21:04
【问题描述】:

我正在尝试在 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 服务器是明确的,因为我可以用我的浏览器向它发出请求,它可以持续数小时而不会出现问题。

【问题讨论】:

    标签: php curl httprequest


    【解决方案1】:

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

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

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

    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 所需的时间略高的时间。

    【讨论】:

      【解决方案2】:

      要设置请求标头,请使用 CURLOPT_HTTPHEADER 而不是 CURLOPT_HEADER。

      【讨论】:

        猜你喜欢
        • 2013-11-18
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 2011-01-12
        • 2015-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多