php 查询百度收录,PHP查询百度收录与排名

/*

* 收录、排名

*

* @access public

* @return void

*/

public function checkGather(){

$keyword = urldecode(I('get.txt'));

$url = urldecode(I('get.url'));

$baiduIP = I('get.baiduIP') ? I('get.baiduIP') : 'www.baidu.com';

if(empty($keyword) || empty($url)){

header('HTTP/1.0 404 Not Found');

exit();

}

$data = array('index_state'=>0, 'gather_state'=>0);

//获得网址内容

$gatherContent = @file_get_contents("http://{$baiduIP}/s?wd=". urlencode($url) ."&pn=0");

if(strstr($gatherContent, '验证码')){

preg_match('/]*name="url"[^>]*value="([^>]*)">/', $gatherContent, $baiduUrl);

$gatherContent = @file_get_contents($baiduUrl[1]);

}

//检查是否收录

if(!strstr($gatherContent, iconv('gbk','utf-8','没有找到该URL')) && !strstr($gatherContent, iconv('gbk','utf-8','抱歉,没有找到'))){

$data['gather_state'] = 1;

//获得首页内容

$enKeyword = urlencode($keyword);

$indexContent = @file_get_contents("http://{$baiduIP}/s?wd=$enKeyword&pn=0");

//首页内容是否出现该域名

$ulrArray = explode('.', $url);

$domain = (count($ulrArray) <= 2) ? str_replace('http://', '', $ulrArray[0]) : $ulrArray[1];

preg_match("/$domain/",$indexContent,$domainExists);

if($domainExists){

$indexArray = preg_split('/class="result[^>]*?id="\d{1,2}"/', $indexContent);

array_shift($indexArray);

//循环检查是否存在首页

$p = 0;

$regexCache = '/]*href="([^\s]*)"[^>]*>/';

foreach($indexArray as $v){

$p++;

//获得快照内容

preg_match($regexCache, $v, $indexCacheResult);

$header = get_headers($indexCacheResult[1],1);

$domain = is_array($header['Location']) ? $header['Location'][0] : $header['Location'];

//匹配URL

if($url == $domain){

$data['index_state'] = $p;

break;

}

}

unset($gatherContent);

unset($indexContent);

unset($indexCacheContent);

}

}

echo json_encode($data);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值