1.首頁是圖片處理頁面downpic.php
<?php
function getImage($url,$filename="") {
if($url=="") return false;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
$filename=date("YmdHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
2.通過curl抓取並下載到本地文件夾
<?php
header("Content-Type:text/html;charset=utf-8");
ini_set('max_execution_time', 86400 * 30);//設置時間,以免超時中斷
include 'downpic.php';
$url='http://casarteshoot.sinaapp.com/daochupic';//讀取圖片地址
$curl=curl_init();//初始化curl
/*設置選項*/
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0");
$data=curl_exec($curl);//執行並獲取html內容
$data=json_decode($data);//轉化數據格式($url輸出的是json格式)
curl_close($curl);//釋放句柄
$i=0;
foreach($data as $o){
$i++;
$name=$o->uid;
$time=$o->time;
getImage($o->url,'./mypic4/'.$name.'_'.$time.'.jpg');//保存到指定文件夾中
}
?>