百度资源平台普通收录工具可以向百度搜索主动推送资源,缩短爬虫发现网站链接的时间,不保证收录和展现效果。出于优化本站考虑,对本站小改造一下,当发布出去文章之后立即提交推送当前文章。
找到后台文章保存的方法修改如下:
/**
* 保存文章
* application\admin\controller\Article.php
*/
public function save()
{
if ($this->request->isPost()) {
$data = $this->request->param();
$validate_result = $this->validate($data, 'Article');
if ($validate_result !== true) {
$this->error($validate_result);
} else {
//文章标题重复检测
if (article_title_duplicate_detection($data['title'])) {
if ($this->article_model->allowField(true)->save($data)) {
// 获取当前文章ID
$article = new ArticleModel;
$currentArticleId = $article->getLastInsID();
// $this->success('保存成功');
$this->success(baidu_regular_push($currentArticleId));
} else {
$this->error('保存失败');
}
} else {
$this->error('文章标题已经存在!');
}
}
}
}当文章保存成功的时候将原先的只返回保存结果信息改成一个处理推送加返回信息的方法baidu_regular_push。这个方法接受的参数是当前的文章ID,然后处理对应的文章推送。这个方法写入公共函数文件common.php中:
/**
* 根据文章ID 推送文章到百度
* @param $id 文章ID
*/
use app\common\helper\UrlHelper;
function baidu_regular_push($id)
{
$articleUrl = "https://chaihongjun.me" . UrlHelper::generateArticleUrl($id);
$api = 'http://data.zz.baidu.com/urls?site=https://chaihongjun.me&token=这里是你的token值';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $articleUrl, //推送的文章链接
CURLOPT_TIMEOUT => 10, //超过10秒自动关闭,保证资源不浪费
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result = json_decode($result);
//成功推送一条
if ($result->success == 1) {
return "保存并推送百度成功!当天剩余的可推送数:" . $result->remain;
} else {
return "保存成功,但推送百度失败!错误:" . $result->message;
}
// 关闭cURL会话
// curl_close($ch);
}UrlHelper类的静态方法generateArticleUrl通过文章ID获取文章URL。
完成以上的编辑配置之后,每当保存一篇新的文章成功,则提示推送反馈信息。
然后更新文章页需要修改,因为有时候第一次编辑文章并没有完成好内容,只是暂时保存在数据库,因此更新文章的功能的时候可能也需要推送功能:
public function update($id)
{
if ($this->request->isPost()) {
$data = $this->request->param();
$validate_result = $this->validate($data, 'Article');
//数据验证错误
if ($validate_result !== true) {
$this->error($validate_result);
} else {
// 保存数据库成功
if ($this->article_model->allowField(true)->save($data, $id) !== false) {
//推送到百度
if ($data['push_baidu'] == 1 && $data['status'] == 1) {
$this->success(baidu_regular_push($id, 0));
}
// 不推送 直接保存
else {
$this->success('文章没有推送百度,更新成功!');
}
} else {
$this->error('文章没有推送百度,更新失败');
}
}
}
}然后修改一下后台的编辑模板(edit.html):
<div class="layui-form-item">
<label class="layui-form-label">百度推送</label>
<div class="layui-input-block">
<!-- 如果已经推送过,那么推送和不推送按钮都不可使用 -->
{if condition="$article.push_baidu==1"}
<input type="radio" name="push_baidu" value="1" title="要推送" checked="checked" disabled>
<input type="radio" name="push_baidu" value="0" title="不推送" disabled>
{else /}
<!-- 否则 -->
<input type="radio" name="push_baidu" value="1" title="要推送">
<input type="radio" name="push_baidu" value="0" title="不推送" checked="checked">
{/if}
</div>
</div> 




