抓取网页的里面的所有图片
<?php
download_images('http://gp.qzd18.cn/01/');
function download_images($article_url = '', $image_path = 'tmp'){
// 获取文章类容
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $article_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
$content = curl_exec($ch);
//$content = file_get_contents($article_url);这种老得方式直接放弃
// 利用正则表达式得到图片链接
$reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif|png)).*?>/';
$ret = preg_match_all($reg_tag, $content, $match_result);
$pic_url_array = array_unique($match_result[1]);
// 创建路径
$dir = getcwd() . DIRECTORY_SEPARATOR .'tmp';
if(!file_exists($dir)){
mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
}
foreach($pic_url_array as $pic_url){
$tule_pic=$article_url.$pic_url;
// 获取文件信息
$ch = curl_init($tule_pic);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fileInfo = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
// 获取图片文件后缀
$ext = strrchr($tule_pic, '/');//strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。用于截取后缀
$filename = $dir.$ext;
// 保存图片信息到文件
$local_file = fopen($filename, 'w');
if(false !== $local_file){
if( false !== fwrite($local_file, $fileInfo) ){
fclose($local_file);
}
}
}
}