听说群友想要我网站TDK描述查询接口的源码,便发布出来吧
下方直接上代码:
<?php
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Content-type: application/json');
error_reporting(0);
if(!$_REQUEST['url']) {
exit(json_encode(array("code"=>202,"msg"=>"请输入查询网址"),JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); }
$url=url($_REQUEST['url']);
$return=curl($url);
if(!$return){ exit(json_encode(array("code"=>201,"msg"=>"目标网站无法打开"),JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); }
$return=str_replace(array("\r\n","\r","\n"," "),'',$return);
$return=str_replace(array("'",'"'),'_--_',$return);
$coding=mb_detect_encoding($return,array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
if($coding!="UTF-8") {
$return=mb_convert_encoding($return,'UTF-8',$coding);
}
preg_match('/<title>(.*?)<\/title>/i',$return,$title);
preg_match('/description_--_content=_--_(.*?)_--_/>/i',$return,$description);
if(!$description[1]) {
preg_match('/description_--_content=_--_(.*?)_--_>/i',$return,$description);
}
if(!$description[1]) {
preg_match('/descriptioncontent=(.*?)>/i',$return,$description);
}
preg_match('/keywords_--_content=_--_(.*?)_--_/>/i',$return,$keywords);
if(!$keywords[1]) {
preg_match('/keywords_--_content=_--_(.*?)_--_>/i',$return,$keywords);
}
if(!$keywords[1]) {
preg_match('/keywordscontent=(.*?)>/i',$return,$keywords);
}
preg_match('/author_--_content=_--_(.*?)_--_/>/i',$return,$author);
if(!$author[1]) {
preg_match('/author_--_content=_--_(.*?)_--_>/i',$return,$author);
}
if(!$author[1]) {
preg_match('/authorcontent=(.*?)>/i',$return,$author);
}
preg_match('/founder_--_content=_--_(.*?)_--_/>/i',$return,$founder);
if(!$founder[1]) {
preg_match('/founder_--_content=_--_(.*?)_--_>/i',$return,$founder);
}
if(!$founder[1]) {
preg_match('/foundercontent=(.*?)>/i',$return,$founder);
}
$description=explode('_--_',$description[1]);
$keywords=explode('_--_',$keywords[1]);
$array=array(
"code"=>200,
"msg"=>"查询成功",
"url"=>$url,
'title'=>$title[1],
'description'=>$description[0],
'keywords'=>$keywords[0],
);
echo json(null,$array);
function json($code,$name,$msg=false) {
if(is_array($name)) {
//是数组
$foundation=array(
'code'=>$code,
);
$array=array_merge($foundation,$name);
} else {
//不是数组
header('Content-type: application/json');
$array=array(
'code'=>$code,
$name=>$msg,
);
}
return json_encode($array,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
function getip_user() {
if(empty($_SERVER["HTTP_CLIENT_IP"]) == false) {
$cip = $_SERVER["HTTP_CLIENT_IP"];
} else if(empty($_SERVER["HTTP_X_FORWARDED_FOR"]) == false) {
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if(empty($_SERVER["REMOTE_ADDR"]) == false) {
$cip = $_SERVER["REMOTE_ADDR"];
} else {
$cip = "";
}
preg_match("/[\d\.]{7,15}/", $cip, $cips);
$cip = isset($cips[0]) ? $cips[0] : "";
unset($cips);
return $cip;
}
function curl($url,$data=0,$header_array=0,$referer=0,$time=30,$code=0) {
if($header_array==0) {
$header=array("CLIENT-IP: ".getip_user(),"X-FORWARDED-FOR: ".getip_user(),'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36');
} else {
$header=array("CLIENT-IP: ".getip_user(),"X-FORWARDED-FOR: ".getip_user(),'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36');
$header=array_merge($header_array,$header);
}
//print_r($header);
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
if($data) {
if(is_array($data)){
$data=http_build_query($data);
}
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
if($referer) {
curl_setopt($curl,CURLOPT_REFERER,$referer);
}
curl_setopt($curl,CURLOPT_TIMEOUT,$time);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');
if($code) {
curl_setopt($curl, CURLOPT_HEADER, 1);
$return=curl_exec($curl);
$code_code=curl_getinfo($curl);
curl_close($curl);
$code_int['exec']=substr($return,$code_code["header_size"]);
$code_int['code']=$code_code["http_code"];
$code_int['content_type']=$code_code["content_type"];
$code_int['header']=substr($return,0,$code_code["header_size"]);
return $code_int;
} else {
$return=curl_exec($curl);
curl_close($curl);
return $return;
}
}
function url($url) {
$url=parse_url($url);
if($url["port"]) {
if($url["host"]) {
$url=$url["host"].":".$url["port"];
} else {
$url=$url["path"].":".$url["port"];
}
} else {
if($url["host"]) {
$url=$url["host"];
} else {
$url=$url["path"];
}
}
return $url;
}
使用示例:index.php?url=https://yuanxiapi.cn
评论
发表评论: