代码如下:
<?php namespace app\api\controller; use fast\Http;//引入这个类 use app\common\controller\Api; /** * 示例接口 */ class Youchong extends Api { //如果$noNeedLogin为空表示所有接口都需要登录才能请求 //如果$noNeedRight为空表示所有接口都需要验证权限才能请求 //如果接口已经设置无需登录,那也就无需鉴权了 // // 无需登录的接口,*表示全部 protected $noNeedLogin = ['*']; // 无需鉴权的接口,*表示全部 protected $noNeedRight = ['*']; /** * 测试方法 * * @ApiTitle (测试名称) * @ApiSummary (测试描述信息) * @ApiMethod (POST) * @ApiRoute (/api/demo/test/id/{id}/name/{name}) * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") * @ApiParams (name="id", type="integer", required=true, description="会员ID") * @ApiParams (name="name", type="string", required=true, description="用户名") * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据") * @ApiReturnParams (name="code", type="integer", required=true, sample="0") * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") * @ApiReturn ({ 'code':'1', 'msg':'返回成功' }) */ public function test() { $this->domain = 'http://www.yc.cn'; $typec=['gougou'=>'狗狗','maomao'=>'猫猫','tuzi'=>'兔子','shulei'=>'鼠类','chongwuniao'=>'宠物鸟','chongwugui'=>'宠物龟','chongwuyu'=>'宠物鱼','qita'=>'其他']; $now_path = getcwd();//运行目录 $type=0;//这个是种类的 foreach ($typec as $enname => $zhname) {//循环分类 $type++; // if($type==1 || $type==2){ // continue; //如果不需要录入就跳出 // } $url = $this->domain.'/pet/'.$enname.'/'; echo $zhname; // $get = $Http->get($this->domain.'/pet-all/dog/'); $max_page = $this->pd_max_page($url); // 当前的宠物类别的目录 $cwpath = $now_path.'/yc/'.$zhname; $this->pd_dir($cwpath); for ($i=1; $i <=intval($max_page) ; $i++) { //循环页码 //循环下图片 //打开网页 获取源码 下载图片 //http://www.yc.cn/api/searchPetData.do?petRaceId=1&pageNum=2&pageSize=8&keyword=&baseInfo=&detailInfo=&jsonCallback=jQuery111309024093557830344_1574994785987&_=1574994785988 list($t1, $t2) = explode(' ', microtime()); $time = $t2 . '.' . ceil( ($t1 * 1000) ); $apiurl = $this->domain.'/api/searchPetData.do?petRaceId='.$type.'&pageNum='.$i.'&pageSize=8&keyword=&baseInfo=&detailInfo=&jsonCallback=jQuery111309024093557830344_'.$time.'&_='.$time; // dump($apiurl); $yuanma = $this->get($apiurl); $endstr = $this->getstr($yuanma,'(',')');//取中间文本 $data = json_decode($endstr,true); // dump($data['list']); if(sizeof($data['list'])>0){//如果有数据才循环 foreach ($data['list'] as $key => $value) { // dump($value['showImage']); // dump($value['name']); // exit(); $name = $value['name']; $this->download($value['showImage'],$cwpath,$name.".png",$name); // $img_arr = $this->re_img($yuanma); // foreach($img_arr as $name=>$vurl){ // // // // exit(); // $path = $cwpath.'/'.$name; // // file_put_contents($path,$vurl); // $this->download($vurl,$cwpath,$name.".png",$name); // } } } } // dump($max_page); sleep(4); echo "<br>{$zhname}结束了"; } // $this->success('返回成功QQ496631085', $this->request->param()); } /** * CURL发送Request请求,含POST和REQUEST * @param string $url 请求的链接 * @param mixed $params 传递的参数 * @param string $method 请求的方法 * @param mixed $options CURL的参数 * @return array */ public function post($url,$params){ $Http= new Http(); $yuanma = $Http->sendRequest($url,$params); return $yuanma; // sendRequest } public function getstr($str, $leftStr, $rightStr) { // dump($str); $left=strpos($str,$leftStr); $right = strrpos($str, $rightStr,$left); if($left < 0 or $right < $left) return ''; return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr)); } /** * 找最大页码 * */ public function pd_max_page($url) { // $Http= new Http(); // $yuanma = $Http->get($url); dump($url); $yuanma = $this->get($url); //正则 var count = '251'; $lastPattern ='/count = \'([0-9]{1,})\'\;/'; preg_match_all($lastPattern, $yuanma, $end);//找期数 // dump($end); // dump($yuanma); $max = 1; foreach ($end[1] as $key => $value) { if(intval($value)>$max){ $max = $value; } } // halt($max); $max1 = $max/8; if($max%8>0){ $max1++; } return $max1; } /** * 正则图片地址 * */ public function re_img($str) {//<img alt="柯基犬" src="http://img.boqiicdn.com/Data/BK/P/imagick3061541499071.png" /> //<a target="_blank" href="http://www.boqii.com/entry/detail/425.html"><img alt="泰迪犬" src="http://img.boqiicdn.com/Data/BK/P/imagick441541497241.jpg" /></a> $re ='/\<a .*?href="(.*?)"\>\<img alt="(.*?)" src="(.*?)" \/\>\<\/a\>/'; preg_match_all($re, $str, $end);//找期数 if($end){ $all = array(); foreach($end[2] as $key => $zhname){ $all[$zhname] = $end[3][$key]; } return $all; }else{ return 0; } } //判断目录是否存在,不存在就创建 public function pd_dir($path){ if (!is_dir($path)){ //第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码 $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ echo "目录 $path 创建成功<br>"; }else{ echo "目录 $path 创建失败"; exit(); } } } //get获取源码 public function get($url){ $Http= new Http(); $yuanma = $Http->get($url); return $yuanma; } //下载图片的方法 ,待优化,随便拿取的一个, public function download($url, $save_dir = './yc/',$filename='',$name) { if(trim($save_dir)=='') $save_dir='./yc/'; if(trim($filename)==''){//保存文件名 $allowExt = array('gif', 'jpg', 'jpeg', 'png', 'bmp'); $ext=strrchr($url,'.'); if(!in_array($ext,$allowExt)) return array('file_name'=>'','save_path'=>'','error'=>3); $filename=time().$ext; } if(0!==strrpos($save_dir,'/')) $save_dir.='/'; //创建保存目录 if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)) return array('file_name'=>'','save_path'=>'','error'=>5); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $filename = pathinfo($url); $resource = fopen($save_dir . $name.'.png', 'a'); fwrite($resource, $file); fclose($resource); unset($file,$url); // return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0); } }
里面有很多方法没有删除