版权声明:本文为Areom原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanjianjiu/article/details/79778343
class Spider {
public function downloadImage($url, $path='imagesjpg/')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
$file = curl_exec($ch);
curl_close($ch);
$this->saveAsImage($url, $file, $path);
}
private function saveAsImage($url, $file, $path)
{
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
}
$spider = new Spider();
for ($i=1;$i<300;$i++) {//直接循环生成路径然后下载到本地同目录下(./imagesjpg/)中
$url="http://***/".$i.".jpg";//图片路径
$spider->downloadImage($url);
}