零基础百度收录API/sitemap使用示例

2022/1/23:更新为 Markdown 格式。

本文提供PHPAPI与sitemap的使用示例。但API-PHP的代码推荐配套一个爬取网站网址的代码使用,不然效率不如手动提交高。sitemap部分的代码只适用于wordpress网站,但提交方法一样。

百度收录连接:https://ziyuan.baidu.com/linksubmit/index

API-PHP推送使用示例

1.复制百度收录内的php推送示例中的全部代码,建立一个baiduapi.php的文件,将内容粘贴进去保存。

2.将http://www.example.com/1.html修改为想要推送的连接,如需推送多个,按照以下格式修改,符号均需为英文

array(<br></br> '连接地址1',<br></br> '连接地址2', <br></br>)

3.将文件上传至网站根目录,输入域名/baiduapi.php打开,以本站为例,网站根目录在/wwwroot/中,因此将baiduapi.php上传至/wwwroot/内,输入mwhls.top/baiduapi.php打开。

4.打开后如出现以下代码或类似代码即成功,如提示error:401,可百度相关错误解决。

{ remain:2998, success:2, }

sitemap使用示例

0.附录代码仅适用于wordpress网站。

1.创建一个sitemap.php文件,将文章末尾代码复制进去。

2.将文件上传至WordPress安装目录,输入域名/wordpress/sitemap.php打开,以本站为例,WordPress安装目录在/wwwroot/wordpress/中,因此将sitemap.php上传至/wwwroot/wordpress/内,输入mwhls.top/wordpress/sitemap.php打开。打开后会出现自己网页各页面的信息,即成功。

3.将地址复制进百度收录中sitemap的文本框内即可。以本站为例,将mwhls.top/wordpress/sitemap.php粘贴进sitemap的文本框内,点击提交,等待几天即可在该页面看到提交情况。

sitemap.php代码(代码提供:学做网站论坛):

直接下载:https://mwhls.lanzous.com/igLjKf0wc5e,下载完需将txt改为php格式。

<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 学做网站论坛(https://www.xuewangzhan.net/)-->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页面 */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* 文章循环结束 */ ?>
<?php
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) { ?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php }} /* 单页面循环结束 */ ?>
<?php
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
<url>
<loc><?php echo get_term_link($term, $term->slug); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php }} /* 分类循环结束 */?>
<?php
/* 标签(可选) */
$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;
?>
<url>
<loc><?php echo $link ?></loc>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php } /* 标签循环结束 */ ?>
</urlset>

This article show an explore of baidu php api and sitemap.

Using php api, I recommend using an spider getting website url as its match, since it not as efficient as manual submisson.

And sitemap part show a generic process, but the sitemap file only suit WordPress.

baidu linksubmit:https://ziyuan.baidu.com/linksubmit/index

Php api

1.Copy all the code of php-api example in baidu-linksubmit to a new file baiduapi.php.

2.Replace http://www.example.com/1.htmlwith links you want to submit. here are an example.

array(<br></br> 'link1',<br></br> 'link2', <br></br>)

3.Upload the file into website root, open domain/baiduapi.php. For example, website root is /wwwroot/, so, upload baiduapi.php into /wwwroot/, and open mwhls.top/baiduapi.php.

4.The following output is right, and if it show something like error:401, google that code.

{ remain:2998, success:2, }

Sitemap

0.The code using in this page only match wordpress.

1.Copy the code show in the end of page to a new file sitemap.php.

2.Upload sitemap.php into WordPress installation directory, open domain/wordpress/sitemap.php. For example, WordPress installation directory is /wwwroot/wordpress/, so upload sitemap.php into /wwwroot/wordpress/, open mwhls.top/wordpress/sitemap.php. If success, it will show the information of your website.

3.Copy that address into the sitemap text box of baidu-linksubmit. For example, copy mwhls.top/wordpress/sitemap.phpinto sitemap text box, and click the submit button, it will show the result a few days later.

sitemap.php CODE (from: xuewangzhan.net):

Download:https://mwhls.lanzous.com/igLjKf0wc5e, remember rename it to an .php file

<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 学做网站论坛(https://www.xuewangzhan.net/)-->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页面 */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* 文章循环结束 */ ?>
<?php
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) { ?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php }} /* 单页面循环结束 */ ?>
<?php
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
<url>
<loc><?php echo get_term_link($term, $term->slug); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php }} /* 分类循环结束 */?>
<?php
/* 标签(可选) */
$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;
?>
<url>
<loc><?php echo $link ?></loc>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php } /* 标签循环结束 */ ?>
</urlset>

版权声明:
作者:MWHLS
链接:http://panwj.top/226.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>