PHP curl下载图片的方法

PHP curl下载图片的方法


<?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);
}
?>

ps:如果采用file_get_contents方法发生图片下载过慢

原创文章如转载,请注明本文链接: http://newmiracle.cn/?p=2278

发表评论

电子邮件地址不会被公开。 必填项已用*标注