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方法发生图片下载过慢

posted @ 2019-11-14 13:01  newmiracle宇宙  阅读(1136)  评论(0编辑  收藏  举报