typecho博客百度必应提交url

在之前的typecho插件推荐中,我推荐了一个SEO优化插件,posttobingindexnow。插件功能是每次发布文章后将文章的url通过接口自动提交到必应搜索引擎,加快收录速度。百度也有类似的主动提交接口,在安装相关的百度推送插件之前,我都是每次发布完文章去后台手动提交的。

百度手动提交

虽然个人博客发布文章数量和频次都不多。但是手动提交还是既不够方便,也不够优雅。于是想着把posttobingindexnow插件给扩展一下,支持必应和百度的同步推送。首先先看一下posttobingindexnow插件的代码目录

├── EasyHttp
│   ├── Cookie.php
│   ├── Curl.php
│   ├── Encoding.php
│   ├── Error.php
│   ├── Fsockopen.php
│   ├── Proxy.php
│   └── Streams.php
├── EasyHttp.php
├── Plugin.php
└── README.md

可以看到插件基本上就是plugin.php文件,其余均为一个第三方的http实现,是原作者引入了做辅助使用。所以可以直接忽略,只看plugin.php文件。插件代码里面主要有两部分

  1. 后台面板参数注册
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        
        $debug = new Typecho_Widget_Helper_Form_Element_Text('debug', null, '', _t('<h2><a href="https://blog.wangtwothree.com/code/208.html" target="_blank">使用方法</a> | <a href="https://github.com/TwoThreeWang/PostToBingIndexNow" target="_blank">查看Github</a></h2><br />是否启用日志'), '0或空不开,其它开');
        $form->addInput($debug);
        //bing index now key
        $key = new Typecho_Widget_Helper_Form_Element_Text('key', null, '', _t('Bing IndexNow key'), '申请地址:https://www.bing.com/webmasters/indexnow');
        $form->addInput($key);
        //baidu api push token
        $baidu_api = new Typecho_Widget_Helper_Form_Element_Text('baidu_api', null, '', _t('Baidu Api push api'), '申请地址:https://ziyuan.baidu.com');
        $form->addInput($baidu_api);
    }

于是我在里面新增一项百度的api输入即可,这个api可以在百度站长工具后台申请,路径为资源提交-普通收录-API提交,推送接口处会给出你的推送api,一般为以下格式

http://data.zz.baidu.com/urls?site=https://yourdomain.com&token=xxxxxxxx
  1. 发布文章触发提交
    public static function justdoit($contents, $class)
    {
        //如果文章属性为隐藏加入创建时间和修改时间不一致则返回不执行
        if( 'publish' != $contents['visibility']){
            return;
        }
        //必填项如果没填的话直接停止
        if( is_null(Typecho_Widget::widget('Widget_Options')->plugin('PostToBingIndexNow')->key)){
            return;
        }
        //bing
        try {
            post_to_bing_index_now($contents,$class);
        } catch (Exception $e) {
            logInfo($e);
            exit();
        }
        //baidu
        if(is_null(Typecho_Widget::widget('Widget_Options')->plugin('PostToBingIndexNow')->baidu_api)){
            return;
        }
        try {
            post_to_baidu($contents,$class);
        } catch (Exception $e) {
            logInfo($e);
            exit();
        }
    }

在原触发处,新增一个post_to_baidu的实现即可,而这部分的实现非常简单,百度站长的后台已经提供php代码,直接拷贝过来。修改一下url和api即可,百度代码的获取路径为资源提交-普通收录-API提交-推送示例-php推送示例

function post_to_baidu($content,$classa) {
    $urls = array(
        $classa->permalink
    );
    $api = Typecho_Widget::widget('Widget_Options')->plugin('PostToBingIndexNow')->baidu_api;//baidu_api
    $ch = curl_init();
    $options =  array(
        CURLOPT_URL => $api,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => implode("\n", $urls),
        CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
    );
    logInfo($options);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    logInfo($result);
}

至此,改造就完成了。去后台重新覆盖安装插件,停用后重新激活,配置百度的推送api接口地址。勾选debug后,发布一篇文章。如果发送成功,在后台路径的log文件,会看到相应的推送成功的返回。

temp_log/push_bing.log  //日志路径

推送成功返回示例

{
    "remain":99999,
    "success":1,
    "not_same_site":[],
    "not_valid":[]
}

返回各个字段的含义如下

| 字段| 是否必选 | 参数类型 | 说明 |
| --- | --- | --- | --- |
| success | 是 | int | 成功推送的url条数 |
| remain | 是 | int | 当天剩余的可推送url条数 |
| not_same_site | 否 | array | 由于不是本站url而未处理的url列表 |
| not_valid | 否 | array | 不合法的url列表 |

或者等过后一天,去百度站长后台也可以看到对应的提交记录

百度提交后台反馈

获取完整插件

关注本公众号,回复"typecho博客seo"获取插件下载链接。启用后,后台配置插件的必应key,百度api。然后把debug设置为1,并在站点的根目录下新建temp_log文件夹。


微信公众号


 继续浏览关于 seotypecho百度bing 的文章

 本文最后更新于 2024/02/24 10:09:39,可能因经年累月而与现状有所差异

 引用转载请注明: 芒果屋 > 默认分类 > typecho博客百度必应提交url

您直接访问了本站,莫非记住了域名?