参考百度搜索资源平台的 Api 写了个简单的自动推送全部链接。代码如下:
<?php
$urls = array();
require('./wp-blog-header.php');
header('HTTP/1.1 200 OK');
$posts_to_show = 99999; //获取文章数
array_push($urls, 'https://'.$_SERVER['HTTP_HOST']);
/* 文章页面 */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) {
array_push($urls, get_permalink($post->ID));
}
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) {
array_push($urls, get_page_link($page->ID));
}
}
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) {
array_push($urls, get_term_link($term, $term->slug));
}
}
/* 标签 (可选) */
$tags = get_terms("post_tag");
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), "post_tag" );
if ( is_wp_error( $link ) ) {
return false;
$tags[ $key ]->link = $link;
}
array_push($urls, $link);
}
foreach ($urls as $k => $v) {
echo $v;
echo "<br>";
}
$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;
代码在 WP 根目录放置。修改 $api
为你资源平台的接口调用地址 。建议写个复杂一点的文件名,比如 haoziblogbaidutuisong.php
,避免给别人猜到。然后添加一个定时任务,每天监控一次就可以了,效果如下图:
文章评论