如何让php的curl请求永远等待响应(成功发送请求后)
我正在尝试在 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)而超时。但我找不到卷曲选项来帮助解决这个问题,所以我希望它是别的东西。
tomcat 服务器是透明的,因为我可以使用浏览器向它发出请求,该请求可以持续几个小时而不会出现问题。
I am trying to send big zip files to a tomcat application using curl in a php script. Since it is a big zip file it is going to take some time on the tomcat server to unpack the zip file (about 2-5 minutes), but the curl request never waits more than 30 seconds before it just continue as if it had received an empty response.
Code I can reproduce problem with:
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);
The only suspect I have left is that curl makes the request and then times out due to a certain number of seconds elapsing without any bytes being sent back or forth (aka between_bytes_timeout). But I cannot find a curl option to help with that, so I am hoping it is something else.
The tomcat server is in the clear, since I can make a request to it with my browser that can lasts hours without problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能,cURL 只是自动取消请求,因为 Tomcat 解压 zip 时传输速率太低。
如果平均传输速率在
CURLOPT_LOW_SPEED_TIME
秒内低于CURLOPT_LOW_SPEED_LIMIT
字节/秒,就会发生这种情况。尝试添加具有高时间和/或低限制的适当选项,例如:
为了快速测试,我建议使用略高于 Tomcat 解压给定测试 zip 实际需要的时间。
Chances are, that cURL just auto-cancels the request, because of the transfer rate being too low while your Tomcat is unpacking the zip.
This happens if the average transfer rate drops below
CURLOPT_LOW_SPEED_LIMIT
bytes/second forCURLOPT_LOW_SPEED_TIME
seconds.Try adding the appropriate options with a high time and/or low limit, e.g.:
To quicktest I'd recommend to use a TIME slightly higher than your Tomcat really needs to unpack a given test zip.
要设置请求标头,请使用 CURLOPT_HTTPHEADER 而不是 CURLOPT_HEADER。
For setting request headers, use CURLOPT_HTTPHEADER not CURLOPT_HEADER.