选项#1
而是选择二进制/原始数据组合成的变量然后写入,你可以使用CURLOPT_FILE
选项直接显示一个文件到curl下载。
下面是函数:
// takes URL of image and Path for the image as parameter
function download_image1($image_url, $image_file){
$fp = fopen ($image_file, 'w+'); // open file handle
$ch = curl_init($image_url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
curl_setopt($ch, CURLOPT_FILE, $fp); // output to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
// curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints
curl_exec($ch);
curl_close($ch); // closing curl handle
fclose($fp); // closing file handle
}
这里是你应该怎么称呼它:
// test the download function
download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg");
选项#2
现在,如果你想下载非常大的文件,以上情况功能可能不会变得方便。您可以使用下面的函数来处理大文件。另外,如果需要,您可以打印进度(以%
或任何其他格式)。下面的功能是通过使用callback
函数来实现的,该函数将大块数据写入文件中,以进行下载。
// takes URL of image and Path for the image as parameter
function download_image2($image_url){
$ch = curl_init($image_url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, "curl_callback");
// curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints
curl_exec($ch);
curl_close($ch); // closing curl handle
}
/** callback function for curl */
function curl_callback($ch, $bytes){
global $fp;
$len = fwrite($fp, $bytes);
// if you want, you can use any progress printing here
return $len;
}
这里是如何调用该函数:
// test the download function
$image_file = "local_image2.jpg";
$fp = fopen ($image_file, 'w+'); // open file handle
download_image2("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2");
fclose($fp); // closing file handle
谢谢!我会继续尝试这件事情,看看它是否有效。 – David
请记住编码良好的网站将查找用户代理。每个浏览器,平板电脑或手机都会有一个用户代理!如果您仍然无法获取图像,最有可能是因为用户代理检测,请添加...'curl_setopt($ ch,CURLOPT_USERAGENT,'MyImage Collector + http://www.yourdomainname/mybot.html'); '或欺骗一个真正的'curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13)Gecko/20080311 Firefox/2.0.0.13');' – bbullis
像这样保存图像时是否有任何文件大小限制? PHP配置中的文件上传限制是否会影响到这一点? – Foreever