WordPress纯代码给文章添加百度收录判断

admin 技术杂烩 建站相关评论3,0114阅读模式
逸兔系统

利用api接口纯代码给WordPress文章添加百度收录判断

本站的api接口是https://www.520yxl.cn/api/baidu.php?domain=

api接口文件代码如下:代码来自Weifeng's Blog

<?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];
    }
}
?>

使用方法

以知更鸟的Begin主题为例

打开/wp-content/themes/begin/inc下的post-meta.php文件,在if (zm_get_option('baidu_record')) {baidu_record_t();}的下方添加下方代码,约在55行。以及在if (zm_get_option('baidu_record')) {baidu_record_b();}的下方添加下方代码,约在143行。

  1. //文章百度收录判断
  2. $url = get_permalink();
  3. $result = file_get_contents("https://www.520yxl.cn/api/baidu.php?domain=".$url);//此处的api接口连接可以换成自己的
  4. $arr=json_decode($result,true);
  5. if ($arr['code']==200) {
  6. echo '<span class="baidu-r"><a target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'&tn=bds&cl=3&ct=2097152&si=' . $_SERVER['SERVER_NAME']. '&s=on"><i class="be be-baidu"></i>已收录</a></span>';
  7. else {
  8. echo '<span class="baidu-r"><a rel="external nofollow" title="一键提交给百度" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'"><i class="be be-baidu"></i>暂未收录</a></span>';
  9. }

由于知更鸟的Begin主题我开启了go链接,不想关闭,代码中有些链接自动转换成go链接。转换成go链接也不影响代码的使用,有需要的打开下面的按钮进行复制。

可以下载下方的懒人包上传至Begin主题目录进行覆盖。

其他主题使用方法

把下方的代码放在文章页面里就可以了,每个主题的文章页面命名不一样,你们找找看吧。

  1. <?php
  2. //文章百度收录判断
  3. $url = get_permalink();
  4. $result = file_get_contents("https://www.520yxl.cn/api/baidu.php?domain=".$url);//此处的api接口连接可以换成自己的
  5. $arr=json_decode($result,true);
  6. if ($arr['code']==200) {
  7. echo '<span><a target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'&tn=bds&cl=3&ct=2097152&si=' . $_SERVER['SERVER_NAME']. '&s=on"></i>已收录</a></span>';
  8. else {
  9. echo '<span><a rel="external nofollow" title="一键提交给百度" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">暂未收录</a></span>';
  10. } ?>

由于知更鸟的Begin主题我开启了go链接,不想关闭,代码中有些链接自动转换成go链接。转换成go链接也不影响代码的使用,有需要的打开下面的按钮进行复制。

 

 

weinxin
云轩阁V
微信扫一扫关注
admin
天猫超市-休闲食品-领劵199减100