PHP生成SiteMap文件的代碼
分享一例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("../");
-
PHP將XML轉為數組的方法
本文詳細介紹了,在php編程中,將XML數據轉換為數組的詳細過程,剖析了xml轉為php數組的具體方法,感興趣的朋友可以參考學習下。PHPXML轉數組首先,得到一個xml型的對象:複製代碼代碼示例:$resp=$this->c->execute($req,$sessionKey);//獲得xml對象$items=$resp->items;...
-
用PHP實現文件上傳二法
PHP(HypertextPreprocessor)是一種HTML內嵌式的語言(類似IIS上的ASP)。而PHP獨特的語法混合了C、Java、Perl以及PHP式的新語法。它可以比CGI或者Perl更快速的執行動態網頁。除此之外,用PHP寫出來的Web後端CGI程序,可以很輕易的移植到不同的系統平台上。我們在做...
-
PHP如何使用curl發送GET和POST請求
導語:PHP如何使用curl發送GET和POST請求,具體詳情請閲讀下面代碼,更多詳情請關注應屆畢業生考試網。使用CURL發送請求的基本流程使用CURL的PHP擴展完成一個HTTP請求的發送一般有以下幾個步驟:1.初始化連接句柄;2.設置CURL選項;3.執行並獲取結果;4.釋放VURL連接句柄。...
-
PHP常用開發技巧
PHP開發常用技巧能使你在開發過程中快而有效.以下就是小編精心推薦PHP常用開發技巧,希望對大家有幫助!1使用dowhile避免多層if語句嵌套我們直接舉例説明:實現方式①if($name=='hedong'){if($sex=='male'){if($major=='PHP'){$ret='...