分享一例php生成sitemap地圖文件的`代碼,用於生成google sitemap地圖,有需要的朋友參考學習下。
生成sitemap文件
例子:
複製代碼 代碼示例:
<?php
/**
* 生成sitemap文件
* 平常在用的一段代碼,分享給大家
* edit:
*/
require_once('');
$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("");
$xmlnsxsi = $doc -> createAttribute('xmlns:xsi');
$xmlnsxsivalue = $doc -> createTextNode("");
$schemaLocation = $doc -> createAttribute('xsi:schemaLocation');
$schemaLocationvalue = $doc -> createTextNode(" ");
$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."");
//=================================
if($page=="1"){
$main_sitemap = new DOMDocument('1.0', 'utf-8'); // 聲明版本和編碼
$main_sitemap -> formatOutput = true; //格式XML輸出
$sitemapindex = $main_sitemap -> createElement('sitemapindex');
$xmlns = $main_sitemap -> createAttribute('xmlns');
$xmlnsvalue = $main_sitemap -> createTextNode("");
$sitemap = $main_sitemap -> createElement('sitemap'); //創建一個標籤
$loc = $main_sitemap -> createElement('loc'); //創建一個標籤
$id = $main_sitemap -> createAttribute('id'); //創建一個屬性
$newsid = $main_sitemap -> createTextNode($page); //設置屬性內容
$newsco = $main_sitemap -> createTextNode("http://youdomain/map/sitemap_".$page.""); //設置標籤內容
$xmlns -> appendChild($xmlnsvalue);
$sitemapindex -> appendChild($xmlns);
$id -> appendChild($newsid); //繼承屬性
$loc -> appendChild($id); //繼承屬性內容
$loc -> appendChild($newsco); //繼承標籤內容
$sitemap -> appendChild($loc); //繼承子類
$sitemapindex -> appendChild($sitemap);
$main_sitemap -> appendChild($sitemapindex);
$main_sitemap -> save("../");
}else{
$main_sitemap = new DOMDocument('1.0', 'utf-8'); // 聲明版本和編碼
$main_sitemap->preserveWhiteSpace = false;
$main_sitemap -> formatOutput = true; //格式XML輸出
$main_sitemap->load("../");