php检测百度是否收录了网站的文章。
PHP代码:
public function checkBaiduIndex($url=null)
{
$url = I('post.url');
if(empty($url)) exit('网址不能为空');
$header = array (
"Host:www.baidu.com",
"Content-Type:application/x-www-form-urlencoded",
"Connection: keep-alive",
'Referer:http://www.baidu.com',
'User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BIDUBrowser 2.6)'
);
$target_urls = ['http://test.com'.$url,'http://www.test.com'.$url,'http://m.test.com'.$url];
for ($i=0; $i < count($target_urls)-1; $i++) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, 'https://www.baidu.com/s?wd='.urlencode($target_urls[$i]) );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
$content = curl_exec ( $ch );
curl_close ( $ch );
if(strpos($content,'-网站名称')){
$data['status'] = 1;
$data['url'] = $target_urls[$i];
break;
}
}
echo json_encode($data);
}
<script>
// 检测收录
$(function(){
$(".urls").each(function(){
var url = $(this).attr('data');
var index = $(".urls").index(this);
$.ajax({
type: 'POST',
url: '/home/public/checkBaiduIndex',
data: {"url":url} ,
success: function(data){
if(data == null){
$(".urls").eq(index).html('<font style="color:red;" href="javascript:;">未收录</font>');
}else{
if(data.status === 1){
$(".urls").eq(index).html('<a target="_blank" style="color:green;" href="https://www.baidu.com/s?wd='+data.url+'">已收录</a>');
}
}
},
dataType: 'json'
});
});
});
</script>