Typecho文章页添加百度收录检测

8个月前 教程分享 158°C

这篇文章最后更新于06-08 16:49前,您需要注意相关的内容是否还可用,如有疑问请联系作者!

post.php合适的地方添加

    <script>
        function baidu_check(){
            var url= window.location.href
            $.getJSON("http://huue.cc/baidu.php?domain="+url,function(result){ //调用api
                if (result.code == 200) {
                    $('#baidu_icon').removeClass('glyphicon-refresh');
                    $('#baidu_icon').addClass('glyphicon-ok-circle');
                    $('#baidu_result').text('百度已收录');
                }else if(result.code == 403){
                    $('#baidu_icon').removeClass('glyphicon-refresh');
                    $('#baidu_icon').addClass('glyphicon-info-sign');
                    /*$('#baidu_result').text('百度未收录');*/
                    $('#baidu_result').html('<a style="color:red;" rel="external nofollow" title="点击提交收录!" target="_bl
ank" href="https://ziyuan.baidu.com/linksubmit/url?sitename='+url+'">百度未收录</a>');
 
                }else{
                    $('#baidu_icon').removeClass('glyphicon-refresh');
                    $('#baidu_icon').addClass('glyphicon-remove-circle');
                    $('#baidu_result').text('查询收录失败');
                }
            });
        }
        baidu_check();
</script>

在的位置添加

<!--百度收录--> 
<span class="item"><i class="iconfont icon-liulanqi"></i></span><span class="meta-value" id="baidu_result">加载中</span> 

api代码

<?php
/**
 * Baidu
 * @editer: Weifeng
 * @link: https://wfblog.net
 * @version: 1.0
 */
 
error_reporting(0);
header("Access-Control-Allow-Origin:*");
header('Content-type: application/json');
 
$domain = @$_GET['domain'];
if(!isset($domain) || empty($domain) || $domain==''){
    $data = array(
        "code" => false,
        "msg" => "未传入请求参数!"
    );
    echo json_encode($data,JSON_UNESCAPED_UNICODE);
    exit;
}
if(substr($domain, -1) == '/'){
    $domain = substr($domain,0,strlen($domain)-1);
}
 
$data = checkBaidu($domain);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
 
function checkBaidu($url){
    $header = array(
        "Host:www.baidu.com",
        "Content-Type:application/x-www-form-urlencoded",//post请求
        "Connection: keep-alive",
        "Referer:https://www.baidu.com",
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"
    );
    $url = 'https://www.baidu.com/s?ie=UTF-8&wd='.urlencode($url).'&usm=3&rsv_idx=2&rsv_page=1';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    if(strpos($output, '没有找到') || strpos($output, '很抱歉')){
        $data = array(
            "code" => 403,
            "msg" => "该域名暂时未被百度收录!"
        );
    }else{
        $number = GetBetween($output,'<span class="nums_text">百度为您找到相关结果约','个</span>');
        if(empty($number) || $number == 0){
            $number = GetBetween($output,'<b>找到相关结果数约','个</b></p>');
            if(empty($number) || $number == 0){
                $data = array(
                    "code" => false,
                    "msg" => "获取百度收录失败!"
                );
                return $data;
            }
        }
        $data = array(
            "code" => 200,
            "msg" => "该域名已被百度收录!",
            "number" => str_replace(',','',$number)
        );
    }
    return $data;
}
 
function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
}
?>

其他主题在functions.php添加

/

/百度收录
function baidu_check() {
    $url = baidu_url();
    $api = 'https://www.xiaobai666.top/usr/themes/Cuteen/baidu.php?domain='; //更改为你自己的API
    $result = json_decode(file_get_contents($api.$url));
    if($result['code'] == 200){
        echo '百度已收录';
    }elseif($result['code'] == 403){
        echo '<a style="color:red;" rel="external nofollow" title="点击提交收录" target="_blank" href="http://zhanzhang.baidu
.com/sitesubmit/index?sitename='.$url.'">百度未收录</a>';
    }else{
        echo '查询收录失败';
    }
}
function baidu_url(){
    if((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTT
P_X_FORWARDED_PROTO'] == 'https')){
        return 'https'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }else{
        return 'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }
}   
版权说明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。
本文链接:https://huue.cc/index.php/archives/381.html
最后编辑: 21年06月08日

随机内容

网友评论

4天前

大佬啥时候参与max/routing的开发啊,把功能加到routing中 https://github.com/topyao/max-routing

6天前

11111

1个月前

1111

1个月前

1

2个月前

e21e12e12

2个月前

菊花残,满腚伤。ଲ