So I keep running into the same error… I’ve searched for hours to try to find a resolution, but I just can’t seem to find the missing piece. Lots of other people asking about error 7 on stack overflow, but none that were similar to my scenario.
Basically, I’m using cURL to download images being sent through an XML feed. My entire script works, everything runs, the function I’ve written below even downloads thousands of images (upwards to the 3000 range sometimes).
I guess my question is, why, after downloading 3000 images would it just not connect?
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);
}
So in order to get this to work, I had to put a bottleneck on the function to slow it down a bit. As Andrewsi suggested, the remote site was cutting me off for downloading images too quickly. In order to bottleneck the function, I FTP’d each image to a remote server (since this was required anyway).
Final function looks like this:
If you don’t need to ftp somewhere to create the bottleneck, you could use php’s
sleep(seconds) orusleep(microseconds) functions within yourdownloadImagefunction to create a similar bottleneck.sleepdocumentation:http://php.net/manual/en/function.sleep.php
usleepdocumentation:http://php.net/manual/en/function.usleep.php
Hope this theory helps someone else out.