百度提交资源有三种方式:
- api提交
- sitemap方式
- 手动提交方式

第一种方式我最喜欢,因为可以做到完全自动提交。第二种方式看起来都费劲,一堆规则。第三种方式太原始了。
下面我们讲第一种方式如何实现完全自动提交:
1.准备一个脚本
项目根目录下新建一个sitemap.php文件,将$siteName和$api换成你自己的域名和百度给你的api地址,$dbHost、$database、$userName、$passwd换成你自己的数据库信息。
<?php
$siteName = 'http://ruanwenwu.com.cn';
$api = 'http://data.zz.baidu.com/urls?site=ruanwenwu.com.cn&token=LYOJnK1uSeveZEM';
$fileName = "./sitemappost.txt";
$dbHost = 'dbhost';
$database = 'awen_database';
$userName = 'awen';
$passwd = 'awen';
$urls = array();
$pageSize = 10;
if(!file_exists($fileName)){
touch($fileName);
file_put_contents($fileName,0);
}
$minId = file_get_contents($fileName);
$link = mysqli_connect($dbHost,$userName,$passwd);
mysqli_query($link,$database);
mysqli_query($link,'set names utf8');
$res = mysqli_query($link,"select id from wp_posts where post_status = 'publish' and post_type = 'post' and id > '{$minId}' order by id asc ");
if($res) {
while ($data = mysqli_fetch_assoc($res)) {
$minId = $data['id'];
$urls[] = "{$siteName}/?p=" . $data['id'];
}
$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);
file_put_contents($fileName,$minId);
echo $result;
}
2.放到计划任务中
在linux系统中,使用crontab -e命令打开自动运行控制台,输入下面这一行:
* 1 * * * curl ruanwenwu.com.cn/sitemap.php
这句话的意思是每天凌晨1点请求一次这个sitemap.php提交最新的文章到百度资源。
注意:此脚本不适用于大量数据场景,如果你是刚建站,可以放心使用。有疑问的话,欢迎和阿文交流。