PHP添加百度收录判断以及未收录自动提交

11/15/2021 php

# 开篇

本来是想自己写一个插件来做百度收录的,但是发现typecho的教程不是很完善,百度谷歌搜了几个基本上都是一摸一样的,所以对我这样没基础的小白是有点难度的,虽然说多看几个插件的源码,应该能搞懂大致的意思,但是比较费时间。

所以还是准备写一下php的代码,不过其实我之前写过一次了,所以现在重新写一遍而已,并且投入线上使用。

# 代码

不过其实还是不是非常的完善,具体原因是因为php提交数据,百度的查询有点小问题,经常会出现一个验证的系统,所以可以考虑不管是否收录都提交,反正咱们只是小站而已。

<?php
        $url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        function CheckBaiduUrl($url){
            $urls = array($url);
            $header = array (
                "Host:www.baidu.com",
                "Content-Type: text/html;charset=utf-8",
                "Connection: keep-alive",
                'Referer:https://www.baidu.com',
                'Content-Encoding: gzip',
                'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
            );
            $url = 'http://www.baidu.com/s?wd='.$url;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $rs=curl_exec($curl);
            curl_close($curl);
            $api = 'http://data.zz.baidu.com/urls?site=https://www.datehoer.com&token=XHWvdkaWTnFP9gyu';
            $options =  array(
                CURLOPT_URL => $api,
                CURLOPT_POST => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => implode("\n", $urls),
                CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
            );
            $ch = curl_init();
            if(!strpos($rs,'提交网址')){

            }else{
                echo "<script>console.log('未收录')</script>";
                curl_setopt_array($ch, $options);
                $result = curl_exec($ch);
                $file = fopen('info.text',"a+");
                fwrite($file,$result.date("Y/m/d"));
                fwrite($file,"\r\n");
            }
        }
        CheckBaiduUrl($url);
    ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42