在循环中使用 CURL 下载文件,第一个有效,第二个超时

发布于 2024-09-04 03:56:05 字数 1083 浏览 1 评论 0原文

大家早上好,

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你的占有欲 2024-09-11 03:56:05

尝试在 fclose($f); 之后添加 curl_close($ch);。在我看来,它可能正在尝试重用连接,这反过来又导致了问题。

Try adding curl_close($ch); after fclose($f);. It seems likely to me that it is trying to reuse the connection, which is in return causing the issue.

长伴 2024-09-11 03:56:05

如果您一遍又一遍地访问同一个站点,他们可能会遇到某些问题,导致传入的请求超时太快。如果您正在循环,请尝试在每次迭代后添加一个简短的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文