//抓取页面
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;
}
PHP爬取网页内容并存放至项目文件中
最新推荐文章于 2021-08-27 09:48:46 发布