PHP curl下载图片的方法
PHP curl下载图片的方法
<pre>
<?php
$images = [
'http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKPkia3rxjuBoSQ1sUSmqYXDazibuIp8p2CCialBPjeYvcNJgky9P7hmlnDKgzX91utEQPu7VtsDqAIg/0',
];
function download($url, $path = 'dowloadceshi/2015-09-22_00-17-06j.png')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path, 'a');
fwrite($resource, $file);
fclose($resource);
}
foreach ( $images as $url ) {
download($url);
}
?>
</pre>
ps:如果采用file_get_contents方法发生图片下载过慢
如果遇到什么不懂的地方直接关注公众号留言(本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。)
作者:newmiracle
出处:https://www.cnblogs.com/newmiracle/