使用例子 ?php include imgCatcher.class.php; header(content-type:text/html;charset=utf-8); $catcher = New imgCatcher(http://www.oschina.net/,images,jpg); $catcher-get_pic(); ? 無(wú) ?php class imgCatcher{const FLODER_DIR = 'image/';const IMG_TY
使用例子
<?php class imgCatcher{ const FLODER_DIR = 'image/'; const IMG_TYPE = 'jpg|bmp|png|gif'; var $floder; var $url; var $type; var $domain; var $pic_arr; public function __construct($url,$floder='',$type=''){ $this->url = $url; $url_arr = parse_url($url); $this->domain = array_shift($url_arr); if(trim($floder) == ""){ $this->folder = FLODER_DIR; }else{ $this->folder = $floder.'/'; } if(trim($type)==""){ $this->type = IMG_TYPE; }else{ $this->type = $type; } $this->pic_arr = array(); if(!is_dir(__DIR__.'/'.$this->folder)) mkdir(__DIR__.'/'.$this->folder); } public function get_pic(){ set_time_limit(0);//抓取不受時(shí)間限制 //獲取圖片二進(jìn)制流 $data = self::CurlGet($this->url); //利用正則表達(dá)式得到圖片鏈接 $pattern_src = '/@@##@@type.')).*?>/';// $num = preg_match_all($pattern_src, $data, $match_src); $this->pic_arr=$match_src[1];//獲得圖片數(shù)組 $this->get_name(); return 0; } public function get_name(){ $pic_arr = $this->pic_arr; //圖片編號(hào)和類型 $pattern_type = '/.*\/(.*?)$/'; foreach($pic_arr as $pic_item){//循環(huán)取出每幅圖的地址 $num = preg_match_all($pattern_type,$pic_item,$match_type); //以流的形式保存圖片 $write_fd = fopen($this->folder.$match_type[1][0],"wb"); fwrite($write_fd, self::CurlGet($pic_item,$this->url)); fclose($write_fd); } return 0; } //抓取網(wǎng)頁(yè)內(nèi)容 static function CurlGet($url,$domain=""){ if(substr($url, 0, 1) == "/"){ $url = $domain.$url; } if(substr($url, 0, 1) == "."){ $url = $domain.substr($url, 1); } if(substr($url, 0, 2) == ".."){ $url = $domain.substr($url, 2); } $url=str_replace('&','&',$url); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); //curl_setopt($curl, CURLOPT_REFERER,$url); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)"); curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); $values = curl_exec($curl); curl_close($curl); return $values; } } ?>
PHP怎么學(xué)習(xí)?PHP怎么入門(mén)?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門(mén)到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)