php curl errno为7,PHP / cURL错误7;无法连接到主机

所以为了让这个工作起作用,我不得不在这个函数上放置一个瓶颈来减慢它的速度。正如安德鲁斯建议的那样,远程站点让我过快地下载图像。为了阻止该功能,我将每个图像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) . "
";

//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) . "
";

//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) . "
";

//exit;

}

}

}

curl_close($ch2); //Close the cURL handle that downloads images

fclose($fp);

}如果你不需要在某个地方使用ftp来创建瓶颈,你可以在downloadImage函数中使用php的sleep(秒)或usleep(微秒)函数来创建类似的瓶颈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值