百度Site查詢接口源碼

昨天用公司的企業站發布文章后,想看看之前的網站收錄了沒有,但是又得通過第三方工具查詢有點麻煩,看到crm系統的插件還有一個百度收錄檢測的,我把代碼修改了一下,經過測試web頁面通用,可以做成php插件

html代碼部分

<a id="czmz_baidu" rel="external nofollow" target="_blank"></a>
1
引用的js,必須引用jq文件才能生效

$(function () {
$.getJSON('https://www.czmz.top/usr/query.php?url='+window.location.href, function(json, textStatus) {
//getJSON自己搭建的接口可以放自己的鏈接 上面是我自己搭建的接口
if (json.state == 1) {
$('#czmz_baidu').html('百度已收錄');
$("#czmz_baidu").attr('href','https://www.baidu.com/s?wd='+document.title); }else{
$('#czmz_baidu').html('百度暫未收錄');
$('#czmz_baidu').css('color','red');
$('#czmz_baidu').attr('href','http://zhanzhang.baidu.com/sitesubmit/index?sitename='+window.location.href);
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';}
else {bp.src = 'http://push.zhanzhang.baidu.com/push.js';}
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(bp, s);
}
});
});

php接口

/**
* 百度Site查詢接口
* code 200->正常;201->沒有請求參數
* state 1->收錄;0->未收錄
*/
header("Access-Control-Allow-Origin:*");
header('Content-type: application/json');
if(!isset($_GET['url'])||empty($_GET['url'])||$_GET['url']==''){
echo json_encode(array('code'=>'201','msg'=>'請填寫請求參數'));
exit();
}
// 請求地址www.czmz.cn
$url = $_GET['url'];
// 百度搜索地址http://www.baidu.com/s?wd=site:czmz.top
$baidu='http://www.baidu.com/s?wd='.$url;

$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$baidu);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
$rs=curl_exec($curl);
curl_close($curl);

$str = preg_match_all('/很抱歉,沒有找到與/',$rs,$baidu);

if(!empty($str)){
// 無以下是網頁中包含信息
echo json_encode(array('code'=>'200','url'=>$url,'state'=>'0'));
}else{
// 有以下是網頁中包含信息
$str = preg_match_all('/<font class="c-gray">沒有找到該URL。您可以直接訪問&nbsp;<\/font>/',$rs,$baidu);
if($str){
echo json_encode(array('code'=>'200','url'=>$url,'state'=>'0'));
}else{
echo json_encode(array('code'=>'200','url'=>$url,'state'=>'1'));
}

}

版權聲明:admin 發表于 2022-01-24 17:20:45。
轉載請注明:百度Site查詢接口源碼 | 贏在起跑線

暫無評論

暫無評論...