PHP爬取网页内容并存放至项目文件中

//抓取页面
function curl_info(){
		$s=$this->curl_video_thumb($video_url['video_url']);
        $css_zheng_ze = "/<META\s+property=\"og:image\"\s+content=\"([\w\W]*?)\"/si";
        preg_match($css_zheng_ze,$s,$end);
        if(empty($_POST['info']['thumb'])){
             $_POST['info']['thumb']=APP_PATH.$this->crabImage($end[1]);
        }
}
//CURL请求
function curl_video_thumb($url, $type = 'GET', $data = array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    //curl_setopt($ch, CURLOPT_HEADER, ['Host: m.bilibili.com','User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Mobile Safari/537.36']);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_ENCODING, "");
    if($data){
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
	if(isset($data['file']) && $data['file']){
		curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false);
	}
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
/**
     * @param $imgUrl
     * @param string $saveDir
     * @param null $fileName
     * @return bool|string
     * 将文件移动到uploadfile文件夹
     */
    function crabImage($imgUrl, $saveDir='uploadfile\/bilibili\/', $fileName=null){
        if(empty($imgUrl)){
            return false;
        }

        //获取图片信息大小
        $imgSize = getImageSize($imgUrl);
        if(!in_array($imgSize['mime'],array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'),true)){
            return false;
        }

        //获取后缀名
        $_mime = explode('/', $imgSize['mime']);
        $_ext = '.'.end($_mime);

        if(empty($fileName)){  //生成唯一的文件名
            $fileName = date("YmdHis",time()).mt_rand(111,999).$_ext;
        }

        //开始攫取
        ob_start();
        readfile($imgUrl);
        $imgInfo = ob_get_contents();
        ob_end_clean();

        if(!file_exists(PHPCMS_PATH.$saveDir)){
            mkdir(PHPCMS_PATH.$saveDir,0777,true);
        }
        $fp = fopen(PHPCMS_PATH.$saveDir.$fileName, 'a');
        $imgLen = strlen($imgInfo);    //计算图片源码大小
        $_inx = 1024;   //每次写入1k
        $_time = ceil($imgLen/$_inx);
        for($i=0; $i<$_time; $i++){
            fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));
        }
        fclose($fp);
        return $saveDir.$fileName;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值