百度、bing、搜狗、Google、雅虎的收录量查询API

我之前看别人使用QQ机器人的时候,有一个站长工具插件,但是收费,应该是对接的API,本来我想写的,但是写了一半(只写了API部分)发现没什么用,而且百度的经常要验证。我写了百度、bing、搜狗、Google、雅虎这5个的接口。

返回效果如图:

图片[1]-百度、bing、搜狗、Google、雅虎的收录量查询API - KEKC博客-KEKC博客

百度、bing、搜狗的API代码:

<?php
/*
Plugin Name: XY-百度收录量
Description: XY-根据域名返回百度收录量
*/
$domain = (isset($_GET['domain']))?$_GET['domain']:$_POST['domain'];
if(empty($domain))  echo '查询域名不能为空';

$baiducount = baiduSL ($domain);
$bingcount = bingSL ($domain);
$sogoucount = sogouSL($domain);

$googleyahoodata = file_get_contents("http://www.c4r.cn/sl.php?domain=".$domain);
$googleyahoojson = json_decode($googleyahoodata);

$googlecount = $googleyahoojson -> google;
$yahoocount = $googleyahoojson -> yahoo;

if(!isset($bingcount))  $bingcount = "查询失败";
if(!$bingcount)  $bingcount = 0;

if(!isset($baiducount))  $baiducount = "查询失败";
if(!$baiducount)  $baiducount = 0;

if(!isset($googlecount))  $googlecount = "查询失败";
if(!$googlecount)  $googlecount = 0;

if(!isset($yahoocount))  $yahoocount = "查询失败";
if(!$yahoocount)  $yahoocount = 0;



$result=array(
    'code'=>1,
    'domain'=>$domain,
    'data'=>array(
        'baidu'=>$baiducount,
        'bing'=>$bingcount,
        'google'=>$googlecount,
        'yahoo'=>$yahoocount,
        'sogou'=>$sogoucount,
        )
);
print_r(json_encode($result));


unset($domain,$result,$ch);


function sogouSL ($domain) {
     $sogou='http://www.sogou.com/web?query=site%3A'.$domain; 
     $sogousite=curl($sogou); 
     $sogousite = str_replace(array("\r\n", "\r", "\n", '    '), '', $sogousite); 
     if (!@$count) preg_match('/<p class="sr-num">找到约(.*?)条结果/i',$sogousite,$count);
     $sogouSL=@strip_tags($count[1]); 
     unset($count);
     return $sogouSL;
}

function baiduSL ($domain) {
     $baidu='https://www.baidu.com/s?ie=utf-8&tn=baidu&wd=site%3A'.$domain; 
     $bdsite=curl($baidu); 
     $bdsite = str_replace(array("\r\n", "\r", "\n", '    '), '', $bdsite); 
     if (!@$count) preg_match('/找到相关结果数约(.*?)个/i',$bdsite,$count);
     $baiduSL=@strip_tags($count[1]); 
     unset($count);
     return $baiduSL;
}

function bingSL ($domain) {
     $bing='https://cn.bing.com/search?q=site%3A'.$domain; 
     $bingsite=curl($bing); 
     $bingsite = str_replace(array("\r\n", "\r", "\n", '    '), '', $bingsite); 
     if (!@$count) preg_match('/<span class="sb_count">(.*?) 条结果/i',$bingsite,$count);
     $bingSL=@strip_tags($count[1]); 
     unset($count);
     return $bingSL;
}

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}
?>

Google、雅虎的API代码:

<?php
$domain = (isset($_GET['domain']))?$_GET['domain']:$_POST['domain'];

$yahoocount = yahooSL ($domain);
$googlecount = googleSL ($domain);
$result=array(
        'yahoo'=>$yahoocount,
        'google'=>$googlecount
);
print_r(json_encode($result));

function yahooSL ($domain) {
     $yahoo='https://hk.search.yahoo.com/search;?p=site%3A'.$domain; 
     $yahoosite=curl($yahoo); 
     $bingsite = str_replace(array("\r\n", "\r", "\n", '    '), '', $yahoosite); 
     if (!@$count) preg_match('/大約 (.*?) 個搜尋結果/i',$yahoosite,$count);
     $yahooSL=@strip_tags($count[1]); 
     unset($count);
     return $yahooSL;
}

function googleSL ($domain) {
     $google='https://www.google.com/search?q=site%3A'.$domain; 
     $googlesite=curl($google); 
     $googlesite = str_replace(array("\r\n", "\r", "\n", '    '), '', $googlesite); 
     if (!@$count) preg_match('/約 (.*?) 項搜尋結果/i',$googlesite,$count);
     $googleSL=@strip_tags($count[1]); 
     unset($count);
     return $googleSL;
}

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

至于为什么要分开写,那是因为我服务器国内的,Google、雅虎需要转接一下。

© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容