[英]PHP/cURL Error 7; Couldn't connect to host
因此,我一直遇到相同的错误...我已经花了几个小时试图找到解决方法,但似乎找不到丢失的部分。 许多其他人询问堆栈溢出错误7,但没有一个与我的情况类似。
基本上,我使用cURL下载通过XML feed发送的图像。 我的整个脚本都能正常工作,所有程序都可以运行,我在下面编写的功能甚至可以下载成千上万张图片(有时最多可以下载到3000张图片)。
我想我的问题是,为什么下载3000张图像后,它会无法连接?
function downloadImage($location, $imagesPath, $imageName) {
//Location fix
$location = str_replace(" ", "%20", $location);
$url = $location;
$path = $imagesPath . $imageName;
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
$data = curl_exec($ch);
if ($data === false) {
echo "DownloadImage cURL failed 1: (" . curl_errno($ch) . ") " . curl_error($ch) . "<br/>";
//exit;
}
curl_close($ch);
fclose($fp);
}
因此,为了使其正常工作,我不得不在该功能上加一个瓶颈,以使其稍慢一些。 正如安德鲁西(Andrewsi)所建议的那样,该远程站点由于下载图像太快而使我无法使用。 为了限制该功能,我将每个图像通过FTP传输到远程服务器(因为无论如何都需要这样做)。
最终函数如下所示:
function downloadImage($location, $imagesPath, $imageName, $ch3, $feedFTPinfo) {
//Location fix
//$location = str_replace(" ", "%20", $location);
$url = $location;
$path = $imagesPath . $imageName;
$fp = fopen($path, 'w');
$ch2 = curl_init(); // Initiate cURL for downloading images
//Setup the cURL options for the second handle ($ch2)
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_FILE, $fp);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
//Execute the cURL session
$data2 = curl_exec($ch2);
//Resize Images
$image = new SimpleImage();
$image->load($url);
$imageNameSm = str_replace(".jpg", "", $imageName);
$imageNameSm = $imageNameSm."_sm2.jpg";
$image->resizeToWidth(120);
$image->save($imagesPath . $imageNameSm);
$smPath = $imagesPath . $imageNameSm;
//Find out if there were any issues
if ($data2 === false) {
echo "DownloadImage cURL failed 1: (" . curl_errno($ch2) . ") " . curl_error($ch2) . "<br/>";
//exit;
} else {
//There weren't any issues downloading the file, move it to the speficifed ftp server
if (!empty($feedFTPinfo)) {
$localfile = $path;
$fp = fopen($localfile, 'r');
//Setup the options for the 3rd handle
curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageName);
curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch3, CURLOPT_UPLOAD, 1);
curl_setopt($ch3, CURLOPT_INFILE, $fp);
curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile));
//Execute the 3rd cURL handle
$data3 = curl_exec($ch3);
//Find out if there were any issues with the execution
if ($data3 === false) {
echo "Uploading the image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>";
//exit;
}
$localfile = $smPath;
$fp = fopen($localfile, 'r');
//Setup the options for the 3rd handle
curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageNameSm);
curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch3, CURLOPT_UPLOAD, 1);
curl_setopt($ch3, CURLOPT_INFILE, $fp);
curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile));
//Execute the 3rd cURL handle
$data3 = curl_exec($ch3);
//Find out if there were any issues with the execution
if ($data3 === false) {
echo "Uploading the small image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>";
//exit;
}
}
}
curl_close($ch2); //Close the cURL handle that downloads images
fclose($fp);
}
如果您不需要通过ftp来创建瓶颈,则可以在downloadImage
函数中使用php的sleep
(秒)或usleep
(微秒)函数来创建类似的瓶颈。
sleep
文档: http : //php.net/manual/zh/function.sleep.php usleep
文档: http : //php.net/manual/zh/function.usleep.php 希望这个理论可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.