php 生成 sitemap,PHP生成SiteMap文件代码的方法

PHP生成SiteMap文件代码的方法

分享一例php生成sitemap地图文件的代码,用于生成google sitemap地图,有需要的'朋友参考学习下。

例子:

复制代码 代码示例:

/**

* 生成sitemap文件

* 平常在用的一段代码,分享给大家

* edit: www.jbxue.com

*/

require_once('conn.php');

$doc = new DOMDocument('1.0', 'utf-8');  // 声明版本和编码

$doc -> formatOutput = true;  //格式XML输出

$sql="select mid from dede_member_company";

$query=mysql_query($sql);

$count=mysql_num_rows($query);

$pg=intval($count/3000);

if($count%3000)

$pg++;

$page=isset($_GET['page'])?$_GET['page']:1;

$getpageinfo=page($page,$count,3000);

$sql="SELECT `mid`,`uptime`,`html_path` FROM `dede_member_company` order by `mid`  $getpageinfo[sqllimit]";

$query=mysql_query($sql);

$urlset    = $doc -> createElement('urlset');

$xmlns    = $doc -> createAttribute('xmlns');

$xmlnsvalue  = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");

$xmlnsxsi    = $doc -> createAttribute('xmlns:xsi');

$xmlnsxsivalue  = $doc -> createTextNode("http://www.w3.org/2001/XMLSchema-instance");

$schemaLocation    = $doc -> createAttribute('xsi:schemaLocation');

$schemaLocationvalue  = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");

$xmlns -> appendChild($xmlnsvalue);

$xmlnsxsi -> appendChild($xmlnsxsivalue);

$schemaLocation -> appendChild($schemaLocationvalue);

$urlset       -> appendChild($xmlns);

$urlset       -> appendChild($schemaLocation);

$urlset       -> appendChild($xmlnsxsi);

while($row=mysql_fetch_array($query)){

$url    = $doc -> createElement('url');    //创建一个标签

$loc    = $doc -> createElement('loc');    //创建一个标签

$id    = $doc -> createAttribute('id');      //创建一个属性

$newsid  = $doc -> createTextNode($row[mid]);        //设置属性内容

$newsco = $doc -> createTextNode($row[html_path]);      //设置标签内容

$lastmod    = $doc -> createElement('lastmod');

$modtime  = $doc -> createTextNode(date('c',$row[uptime]));

$changefreq    = $doc -> createElement('changefreq');

$freqtype  = $doc -> createTextNode('daily');

$priority   = $doc -> createElement('priority');

$prival  = $doc -> createTextNode('0.5');

$id        -> appendChild($newsid);    //继承属性

$loc    -> appendChild($id);  //继承属性内容

$loc    -> appendChild($newsco);    //继承标签内容      //继承子类

$lastmod ->appendChild($modtime);

$changefreq ->appendChild($freqtype);

$priority ->appendChild($prival);

$url    -> appendChild($loc);

$url    -> appendChild($lastmod);

$url    -> appendChild($changefreq);

$url    -> appendChild($priority);

$urlset    -> appendChild($url);

}

$doc    -> appendChild($urlset);

$doc    -> save("../map/sitemap_".$page.".xml");

//=================================

if($page=="1"){

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值