因为人懒,所以就想出了这种法子,网上的那些不是使用不了,就是不适合我这种脑子有点生锈的,自己动手!丰衣足食!
本办法利用自己生成的xml文件进行操作,所以不想使用这种方法的可以放弃了。
同时我是直接在根目录操作,如果是在其他目录,那么就需要修改一小点地方就能套用。实在不会就召唤一下我哈。
新建PHP文件命名:site.php
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 500;
$ltime = get_lastpostmodified(GMT);
$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime));
?>
<?php echo '<urlset xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="https://www.sitemaps.org/schemas/sitemap/0.9 https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<?php
/* 文章页面 */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('Y-m-d','c'); ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* 文章循环结束 */ ?>
</urlset>
说实话,一般只需要获取文章的链接提交就行,因为其他页面不需要每天让它自动提交啊。
新建另一个PHP文件,写在网站任何一个文件夹下都行。
命名:showbaidu.php
<?php
$file = 'https://www.slyday.com/site.php';
//将XML中的数据,读取到数组对象中
$xml_object=simplexml_load_file($file);
$xml_json=json_encode($xml_object);//对象转成json
$xml_arr=json_decode($xml_json,true);//json再转成数组
$today = date('Y-m-d',time());
$urls = array();
foreach ($xml_arr as $r){
for ($i = 0; $i < count($r); $i++) {
if ($r[$i]['lastmod'] == $today) {
$urls[] = $r[$i]['loc'];
}
}
}
$api = 'https://data.zz.baidu.com/urls?site=https://www.slyday.com&token=直接百度平台申请';
$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;
?>
大功告成,运行一下,发现没有报错,同时也把当天的链接提交上去了。
那么自动化任务也很简单,如果是appnode,就计划任务,curl 你网址/showbaidu.php,设定好每天自动运行的时间。
如果是宝塔面板,那么就是计划任务,访问链接,填入你网址/showbaidu.php,也设定好每天自动运行的时间。
ok!搞定!