[PHP] 纯文本查看 复制代码
<?php
/*
* File:快手手机壁纸无水印下载保存
* Author:By孤独的疯子
* url:www.chahash.com
*/
//echo GET_CURL('https://v.kuaishou.com/iBwKB0');
echo CheckUrl($_GET['url']);
//检测域名格式
function CheckUrl($C_url){
$str="/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/";
if (!preg_match($str,$C_url)){
echo "域名格式不正确";
}else{
echo GET_CURL($C_url);
}
}
function GET_CURL($url){
$ch = curl_init();//CURL初始化
curl_setopt($ch, CURLOPT_URL, $url);//URL地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//忽略SSL证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//忽略SSL证书
$UA=array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1');
curl_setopt($ch, CURLOPT_HTTPHEADER, $UA);//UA记录
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跳转
$output = curl_exec($ch);//抓取URL并把它传递给浏览器
curl_close($ch);// 关闭cURL资源,并且释放系统资源
return GET_IMG($output);// 返回CURL资源
}
function GET_IMG($data){
$pattern = '/<img.+src=\"(.*?)\" class="object-fit-cover" .*?>/i';//正则规则
preg_match_all($pattern,$data,$match);//正则
$str = str_replace(array('</div>', '<div class="swiper-slide play-images">', ' '), "\n", $match[0]);//替换字符
$str = str_replace('//', 'http://', $str);//转义斜杠
$str = implode($str);//数组转字符串
if($str == NULL){
//return '获取失败';
$pattern = '/<img class="play-long-image" src="(.*?)">/i';//正则规则
preg_match_all($pattern,$data,$match);//正则
$str = str_replace('//', 'http://', $match[1]);//转义斜杠
//$str = implode($str);//数组转字符串
return download($str);
}else{
return GET_PNG($str);
}
}
function GET_PNG($data){
$pattern = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';//正则规则
preg_match_all($pattern,$data,$img);//正则
download($img[1]);
//return json_encode($img[1]);
}
//数组转换下载地址
function download($url){
foreach ($url as $k => $v){
$return_content = downloadImage($v);
//写入数据库sql
}
}
//下载图片
function downloadImage($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$UA=array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1');
curl_setopt($ch, CURLOPT_HTTPHEADER, $UA);//UA记录
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//忽略SSL证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//忽略SSL证书
$file = curl_exec($ch);
curl_close($ch);
saveAsImage($url, $file);
}
//保存图片
function saveAsImage($url, $file){
$filename = pathinfo($url, PATHINFO_BASENAME);
$dirname = date("Y-m-d");
$path = 'uploads/' . $dirname . '/';
$fullpath = 'uploads/' . $dirname . '/' . $filename;
// 如果目录不存在,则创建
if(!is_dir($path)) {
mkdir($path, 0777, true);
}
if(file_exists($fullpath)) {
echo "【已存在】输出路径" . $fullpath .'<br>';
`
}else{
$resource = fopen($fullpath, 'a');
fwrite($resource, $file);
fclose($resource);
echo "【已保存】输出路径" . $fullpath .'<br>';
}
}