<?php
// 抓起365图片
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);//设置PHP超时时间
/**
* 得到当前时间
*/
function getMicrotime() {
list ($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
$stime = getMicrotime();
$callback = $_GET['callback'];
$hrefs = $_GET['hrefs'];
$urlarray = explode(',',$hrefs);
//获取指定url的所有图片
function getimgs($url){
$dirname = basename($url,".php");
if(!file_exists($dirname)){
mkdir('365/'.$dirname.'');
}
clearstatcache();
$data = file_get_contents($url);
preg_match_all("/(href|src)=(["|']?)([^ "'>]+.(jpg|png|PNG|JPG|gif))/2/i", $data, $matches);
//$matches[3] = array_unique($matches[3]);
unset($data);
$i=0;
if(count($matches[3])>0){
foreach($matches[3] as $k=>$v){
//简单判断是否是标准url,而不是相对路径
if(substr($v,0,4)=='http'){
$ext = pathinfo($v,PATHINFO_EXTENSION);//图片扩展
if(!file_exists('365/'.$dirname.'/'.$k.'.'.$ext)){
file_put_contents('365/'.$dirname.'/'.$k.'.'.$ext,file_get_contents($v));
$i++;
}else{
unset($v);
}
clearstatcache();
}else{
unset($v);
}
}
unset($matches);
return $i;
}
}
foreach($urlarray as $k=>$v){
if($v!=''){
$j +=getimgs($v);
}
}
$etime = getMicrotime();
echo "合计采集了".$j."张图片";
echo "用时".($etime-$stime)."秒";