第一步:生成API密钥
打开:https://www.bing.com/indexnow/getstarted#implementation 得到一个txt文件,例如:1ad7ba0***4b64b045fbb****0ac5bfd.txt 将这个文件上传到网站根目录,上传之后不要删除。
第二部:新增bing.php文件
<?php
// 网站的主域名
$host = '域名';
// 配置的key,自己生成32位字符串
$key = '1ad7ba0***4b64b045fbb****0ac5bfd';
// 给平台验证key的链接
$keyLocation = 'https://域名/1ad7ba0***4b64b045fbb****0ac5bfd.txt.txt';$messages = [];
if (!empty($_SERVER['HTTP_REFERER'])) {$url = $_SERVER['HTTP_REFERER'];$messages[] = '推送链接:' . $url;$result = indexNowPush('www.bing.com', [$url]);$messages[] = 'www.bing.com 推送结果:' . ($result ? '成功' : '失败');$result = indexNowPush('api.indexnow.org', [$url]);$messages[] = 'api.indexnow.org 推送结果:' . ($result ? '成功' : '失败');$result = indexNowPush('yandex.com', [$url]);$messages[] = 'yandex.com 推送结果:' . ($result ? '成功' : '失败');
} else {$messages[] = '没有读取到推送链接,无需推送';
}
header('Content-Type: application/javascript');
foreach ($messages as $message) {echo 'console.log("' . $message . '");';
}
/*** indexnow推送* @param string $searchengine 推送的搜索引擎,api.indexnow.org、www.bing.com、yandex.com* @param array $urls 推送网址* @return bool*/
function indexNowPush($searchengine, $urls)
{global $host;global $key;global $keyLocation;if (empty($urls)) {return false;}$data = ['host' => $host,'key' => $key,'keyLocation' => $keyLocation,'urlList' => $urls];$data = json_encode($data, JSON_UNESCAPED_UNICODE);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'https://' . $searchengine . '/indexnow');curl_setopt($ch, CURLOPT_HTTPHEADER, ['Host: ' . $searchengine,'Content-Type: application/json','Content-Length:' . strlen($data)]);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_exec($ch);$httpCode = 0;if (!curl_errno($ch)) {$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);}curl_close($ch);return $httpCode === 200;
}
把上面的代码保存在bing.php文件中,修改为你的域名和第一步生成的API信息。
第三步:添加提交代码
<script src="<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; ?>/bing.php"></script>
将这段代码添加到需要提交的页面中,很多程序都有公共的footer页面,添加在这就行。