IndexNow是网站所有者立即通知搜索引擎其网站上最新内容更改的简单方法。IndexNow是一个简单的ping,这样搜索引擎就知道URL及其内容已被添加、更新或删除,从而使搜索引擎能够在其搜索结果中快速反映这一变化。目前得到微软必应、Naver、Seznam.cz、Yandex、Yep的支持。
以上是官网的介绍,之前博客优化的时候已经将百度推送的功能完成,一旦文章发布完成,并且在后台设置选择了需要推送,那么文章在保存到数据库之后立即推送百度搜索引擎。这里的IndexNow实际和百度推送功能一致,所以就再次优化一下,修改了之前的函数,具体的操作步骤按照官网 https://www.bing.com/indexnow 的介绍来,它会给出一个DEMO:
Request
POST /IndexNow HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: api.indexnow.org
{
"host": "www.example.org",
"key": "64c3f8a3e36d4e33bdecfc3523a16ff5",
"keyLocation": "https://www.example.org/64c3f8a3e36d4e33bdecfc3523a16ff5.txt",
"urlList": [
"https://www.example.org/url1",
"https://www.example.org/folder/url2",
"https://www.example.org/url3"
]
}以上的一串数字是一个key,这个自行申请,按照上面的请求内容,修改之后的推送函数如下:
/**
* 根据文章ID 推送文章到百度和Bing
* @param $id 文章ID
* @param $type 0更新文章时推送 1保存文章时推送
*/
function baidu_regular_push($id, $type)
{
$msg = ['更新', '保存'];
$articleUrl = "https://chaihongjun.me" . UrlHelper::generateArticleUrl($id);
$baidu_ch = curl_init();
$bing_ch = curl_init();
$baidu_api = 'http://data.zz.baidu.com/urls?site=https://chaihongjun.me&token=Gbdb10GHA8NxrioW';
$baidu_options = array(
CURLOPT_URL => $baidu_api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $articleUrl,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($baidu_ch, $baidu_options);
$baidu_result = curl_exec($baidu_ch);
//echo $result;
$baidu_result = json_decode($baidu_result);
curl_close($baidu_ch);
// 增加Bing_IndexNow 推送
$bing_api = 'https://api.indexnow.org/IndexNow';
// 设置POST字段
$data = array(
'host' => 'chaihongjun.me',
'key' => '2108560ea61b4ddf892b3dbe10fed36a',
'keyLocation' => 'https://chaihongjun.me/2108560ea61b4ddf892b3dbe10fed36a.txt',
'urlList' =>[$articleUrl]
);
$json_data = json_encode($data);
//参数
$bing_options = array(
CURLOPT_URL => $bing_api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $json_data,
CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8'),
);
curl_setopt_array($bing_ch, $bing_options);
$response = curl_exec($bing_ch);
// 获取IndexNow响应码
$status_code = curl_getinfo($bing_ch, CURLINFO_HTTP_CODE);
$response = json_decode($response);
curl_close($bing_ch);
//成功推送一条
if (($status_code == 200) && !empty($baidu_result->success)) {
return $msg[$type] . "文章且推送百度和Bing成功!当天Baidu剩余的可推送数:" . $baidu_result->remain;
}
//推送失败
else if ($status_code == 400) {
return $msg[$type] . "Bing Invalid format or 推送百度失败!错误:" . $baidu_result->message;
} else if ($status_code == 403) {
return $msg[$type] . "IndexNow Key invalid or 推送百度失败!错误:" . $baidu_result->message;
} else if ($status_code == 429) {
return $msg[$type] . "Too Many Requests (potential Spam) 推送百度失败!错误:" . $baidu_result->message;
}
}




