在循环中使用 CURL 下载文件,第一个有效,第二个超时
大家早上好,
我正在使用 CURL 在循环中下载图像文件。第一次运行正常,我看到图像出现在目录中。
第二次它会因超时而失败,尽管它是一个有效的 URL。
谁能建议为什么它总是在第二次失败以及如何修复它?
代码片段是:
// download image
$extension = "gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_URL, $imgurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
echo $imgurl . " attempting to open URL ";
$i = curl_exec($ch);
if ( $i==false ) {
echo curl_errno($ch).' '.curl_error($ch);
}
$image_name=time().'.'.$extension;
$f = fopen('/fulldirectorypath/' . $image_name ,'w+');
fwrite($f,$i);
fclose($f);
我在其中放置了一个 echo 来显示 $IMGURL,以检查它是否有效,并将超时时间提高到 90 秒,但它仍然失败。这是我在屏幕上看到的:
http ://images.eu-xmedia.de/thumbnails/34555861/5676051/pt=pic,lang=2,origfile=yes/image.gif 尝试打开 URL 28 操作 90 秒后超时,值为 0 收到的字节数
在我的目录中创建了一个空文件。
非常感谢,
格雷格
Morning all,
I am using CURL to download an image file within a loop. The first time it runs fine and I see the image appear in the directory.
The second time it fails with a timeout, despite it being a valid URL.
Can anyone suggest why it always fails on the 2nd time and how to fix it?
The snippet of code is:
// download image
$extension = "gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_URL, $imgurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
echo $imgurl . " attempting to open URL ";
$i = curl_exec($ch);
if ( $i==false ) {
echo curl_errno($ch).' '.curl_error($ch);
}
$image_name=time().'.'.$extension;
$f = fopen('/fulldirectorypath/' . $image_name ,'w+');
fwrite($f,$i);
fclose($f);
I have put an echo in there to display the $IMGURL, to check it is valid, and upped the timeout to 90 secs, but it still fails. This is what I see on screen:
http://images.eu-xmedia.de/thumbnails/34555861/5676051/pt=pic,lang=2,origfile=yes/image.gif
attempting to open URL 28 Operation
timed out after 90 seconds with 0
bytes received
an empty file is created in my directory.
thanks alot,
Greg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在
fclose($f);
之后添加curl_close($ch);
。在我看来,它可能正在尝试重用连接,这反过来又导致了问题。Try adding
curl_close($ch);
afterfclose($f);
. It seems likely to me that it is trying to reuse the connection, which is in return causing the issue.如果您一遍又一遍地访问同一个站点,他们可能会遇到某些问题,导致传入的请求超时太快。如果您正在循环,请尝试在每次迭代后添加一个简短的
sleep
。您可能应该考虑使用的一件事是多卷曲。它并行运行 Curl 操作。 php.net 上的 Curl 函数 部分记录了它们。我建议查看 本教程。有人可能已经编写了curl 和multi-curl 的面向对象抽象。
If you are hitting the same site over and over, they may have something which times out requests coming in too fast. If you are looping, try adding a brief
sleep
after each iteration.One thing you should probably look in to using is multi-curl. It runs Curl operations in parallel. The section on Curl Functions on php.net documents them. I'd suggest looking at this tutorial. Someone out there has probably written an object-oriented abstraction of curl and multi-curl.