php查询一个网址是否被百度收录,以及标题在百度收录的数量

自己在网上找了一些代码,但都不完善,自己补充了一下。完整代码如下:

function checkBaidu_count($keyword) {
    $keyword = urlencode($keyword);
    $result_arr = array();
    $url = 'http://www.baidu.com/s?wd=' . $keyword;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $rs = curl_exec($curl);
    curl_close($curl);

    $utf_pattern = "/找到相关结果约(.*)个/";
    print_r($rs);die;
    preg_match($utf_pattern,$rs,$all_num);
    return $all_num[1];
}

function checkBaidu_shoulu($url) {
    $result_arr = array();
    $url = 'http://www.baidu.com/s?wd=' . $url;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $rs = curl_exec($curl);
    curl_close($curl);
    $arr = parse_url($url);
    if (strpos($arr['query'], 'http://')) {
        $arr['query'] = str_replace('http://', '', str_replace('wd=', '', $arr['query']));
    } else {
        $arr['query'] = str_replace('wd=', '', $arr['query']);
    }
    if (strpos($arr['query'], '?')) {
        $str = strstr($arr['query'], '?');
        $arr['query'] = str_replace($str, '', $arr['query']);
    }
    if (strpos($arr['query'], '/')) {
        $narr = explode('/', $arr['query']);
        $arr['query'] = $narr[0];
    }
    if(strlen($arr['query'])>22){
        //如果网址长度大于22位,则截取前22位
        $arr['query'] = substr($arr['query'],0,22);
    }
    if (strpos($rs, '<span class="g">'.$arr['query']) || strpos($rs, '<b>'.$arr['query'])) {
        echo 1;   //收录
    } else {
        echo 0;      //未收录
    }

}

echo checkBaidu_count("为西部创客充能 花样年美年广场在行动");
$url = "http://news.dichan.sina.com.cn/2015/05/26/1062193.html";
checkBaidu_shoulu($url);

发表评论

邮箱地址不会被公开。 必填项已用*标注