[scode type="blue"]提要:我的域名长期未被百度收录,而用插件一个一个提交不能每天提交,就像做一个当访问网页时,自动提交网址到搜索引擎的功能。[/scode] 最初是用php代码提交的,截取代码如下: ```php https://www.yuhenm.top/baiduPHP.php?domain=' . $post_list; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); //echo $result; // echo $result; if (strstr($result, "该URL暂时未被百度收录!")) { postBaiduAS($post_list); } else { echo ("已被百度收录"); } } function postBaiduAS($post_list) { $urls = array( $post_list, str_replace("archives", "mip", $post_list), str_replace("archives", "amp", $post_list), ); $api = '百度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'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); // echo $result; echo '百度推送成功'; } $post_list = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; BaiDukk($post_list); ?> ``` ---------- **解析** BaiDukk自动检测当前url是否被收录,没有被收录就自动提交,使用postBaiduAS函数。 ---------- [scode type="green"]可是运行后,一看地址响应时间大多都是600ms,这样严重影响他人访问和seo[/scode] **所以我就用nodejs做了个接口** ```node.js const request = require('request'); const http = require('http'); const axios = require('axios'); axios.defaults.baseURL = 'https://www.yuhenm.top'; //设置请求url axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求类型 //集成百度,必应提交接口 http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); let url_post = "https://www.yuhenm.top" + request.url; if (url_post.split("/")[3] === "index.php") { console.log("请求收录地址",url_post); BaiDuURL_KK(url_post,url_post); } }).listen(200, "0.0.0.0"); function BaiDuURL_KK(url,url_post) { //检测百度是否收录 axios.get('/baiduPHP.php?domain=' + url, {}).then(function (response) { console.log(response.data); if(response.data.msg!=="该URL已被百度收录!"){ postBaiDu_url(url_post); postBin_url(url_post); } }) } function postBin_url(url) { //必应提交api console.log('开始提交必应API'); let myJson = { "siteUrl": "https://www.yuhenm.top", "urlList": [ url ] }; request({ url: 'https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=' + 'keyapi', /* xxx需替换为你的key */ method: "POST", json: true, // <--Very important!!! 允许跨域 body: myJson }, function (error, response, body) { console.log('提交必应结果',body); }); } function postBaiDu_url(url) { console.log('开始提交百度API'); const req = http.request({ host: 'data.zz.baidu.com', path: '/urls?site='+url+'&token=***********', method: 'post', headers: { 'User-Agent': 'curl/7.12.1', "Content-Type": "text/plain", "Content-Length": url.length } }, function(res) { res.setEncoding('utf8'); res.on('data', function(data) { console.log("提交百度API返回结果:", res.statusCode, ' - ', data); }); }); req.on('error', (e) => { console.log('提交百度API请求失败:', e); }); req.write(url) req.end(); } ``` 仔细观察上面的代码,这是我通过谷歌拼凑而出的代码,nodejs没用过,只是会点js。 当我使用http包请求验证是否收录接口,发现出现404,于是我就改为axios请求包。 这些api可以使访问文章时自动提价url到百度和必应。 而后现在nodejs写的api可以达到100多ms,大幅度降低延迟,php没有web使用的多线程功能,所以取消了php的三个提交接口,改为nodejs。 Loading... <div class="tip share">请注意,本文编写于 1787 天前,最后修改于 1785 天前,其中某些信息可能已经过时。</div> <div class="tip inlineBlock info"> 提要:我的域名长期未被百度收录,而用插件一个一个提交不能每天提交,就像做一个当访问网页时,自动提交网址到搜索引擎的功能。 </div> 最初是用php代码提交的,截取代码如下: ```php <?php function BaiDukk($post_list) { $api = '<a href="https://www.yuhenm.top/baiduPHP.php?domain='" title="https://www.yuhenm.top/baiduPHP.php?domain='">https://www.yuhenm.top/baiduPHP.php?domain='</a> . $post_list; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); //echo $result; // echo $result; if (strstr($result, "该URL暂时未被百度收录!")) { postBaiduAS($post_list); } else { echo ("已被百度收录"); } } function postBaiduAS($post_list) { $urls = array( $post_list, str_replace("archives", "mip", $post_list), str_replace("archives", "amp", $post_list), ); $api = '百度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'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); // echo $result; echo '百度推送成功'; } $post_list = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; BaiDukk($post_list); ?> ``` ---------- **解析** BaiDukk自动检测当前url是否被收录,没有被收录就自动提交,使用postBaiduAS函数。 ---------- <div class="tip inlineBlock success"> 可是运行后,一看地址响应时间大多都是600ms,这样严重影响他人访问和seo </div> **所以我就用nodejs做了个接口** ```node.js const request = require('request'); const http = require('http'); const axios = require('axios'); axios.defaults.baseURL = '<a href="https://www.yuhenm.top';" title="https://www.yuhenm.top';">https://www.yuhenm.top';</a> //设置请求url axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求类型 //集成百度,必应提交接口 http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); let url_post = "<a href="https://www.yuhenm.top"" title="https://www.yuhenm.top"">https://www.yuhenm.top"</a> + request.url; if (url_post.split("/")[3] === "index.php") { console.log("请求收录地址",url_post); BaiDuURL_KK(url_post,url_post); } }).listen(200, "0.0.0.0"); function BaiDuURL_KK(url,url_post) { //检测百度是否收录 axios.get('/baiduPHP.php?domain=' + url, {}).then(function (response) { console.log(response.data); if(response.data.msg!=="该URL已被百度收录!"){ postBaiDu_url(url_post); postBin_url(url_post); } }) } function postBin_url(url) { //必应提交api console.log('开始提交必应API'); let myJson = { "siteUrl": "<a href="https://www.yuhenm.top"" title="https://www.yuhenm.top"">https://www.yuhenm.top"</a>, "urlList": [ url ] }; request({ url: 'https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=' + 'keyapi', /* xxx需替换为你的key */ method: "POST", json: true, // <--Very important!!! 允许跨域 body: myJson }, function (error, response, body) { console.log('提交必应结果',body); }); } function postBaiDu_url(url) { console.log('开始提交百度API'); const req = http.request({ host: 'data.zz.baidu.com', path: '/urls?site='+url+'&token=***********', method: 'post', headers: { 'User-Agent': 'curl/7.12.1', "Content-Type": "text/plain", "Content-Length": url.length } }, function(res) { res.setEncoding('utf8'); res.on('data', function(data) { console.log("提交百度API返回结果:", res.statusCode, ' - ', data); }); }); req.on('error', (e) => { console.log('提交百度API请求失败:', e); }); req.write(url) req.end(); } ``` 仔细观察上面的代码,这是我通过谷歌拼凑而出的代码,nodejs没用过,只是会点js。 当我使用http包请求验证是否收录接口,发现出现404,于是我就改为axios请求包。 这些api可以使访问文章时自动提价url到百度和必应。 而后现在nodejs写的api可以达到100多ms,大幅度降低延迟,php没有web使用的多线程功能,所以取消了php的三个提交接口,改为nodejs。 最后修改:2021 年 09 月 14 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏