PHP/cURL错误7;无法连接到主机
问题描述:
因此,我一直遇到同样的错误...我已经搜索了几个小时试图找到一个决议,但我似乎无法找到缺失的一块。很多其他人在堆栈溢出时询问错误7,但没有一个与我的方案相似。PHP/cURL错误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到创建的瓶颈,你可以使用PHP的sleep
(秒)或usleep
(微秒)功能,您downloadImage
功能内造成类似的瓶颈。
-
sleep
文档: http://php.net/manual/en/function.sleep.php -
usleep
文档: http://php.net/manual/en/function.usleep.php
希望这个理论有助于别人了。
远程站点是否会因为太快下载太多图片而中断您的操作? – andrewsi
如果服务器支持它,你应该发送一个保持活动的头,并保持重复使用相同的卷曲手柄进行后续下载。它应该提高性能并可能解决您的问题。 – drew010
$位置是否改变?如果不是,你可以打开连接,然后关闭所有3000+图像完成后关闭? – craig1231