標籤:blog http io ar 使用 for sp 檔案 資料
最近幫朋友最佳化一個網站,想產生xml格式的sitemap然後提交給搜尋引擎,利用php的simpleXML類就很容易實現了。貼一下代碼塊:
//sitemap_data.php 包含了網站所有連結的資訊,直接貼出輸出的資料,源碼就不貼了array(22) { [0]=> array(1) { ["loc"]=> string(32) "http://www.ibxg.com.cn/index.php" } [1]=> array(1) { ["loc"]=> string(32) "http://www.ibxg.com.cn/about.php" } [2]=> array(1) { ["loc"]=> string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=1" } [3]=> array(1) { ["loc"]=> string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=2" } [4]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=1" } [5]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=2" } [6]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=3" } [7]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=4" } [8]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=5" } [9]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=6" } [10]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=7" } [11]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=8" } [12]=> array(1) { ["loc"]=> string(43) "http://www.ibxg.com.cn/product.php?cat_id=9" } [13]=> array(1) { ["loc"]=> string(32) "http://www.ibxg.com.cn/order.php" } [14]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=1" } [15]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=2" } [16]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=3" } [17]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=4" } [18]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=5" } [19]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=6" } [20]=> array(1) { ["loc"]=> string(47) "http://www.ibxg.com.cn/project.php?project_id=7" } [21]=> array(1) { ["loc"]=> string(34) "http://www.ibxg.com.cn/contact.php" }}
sitemap_xml.php檔案
<?phpheader(‘Content-Type: text/xml‘);//這行很重要,php預設輸出text/html格式的檔案,所//以這裡明確告訴瀏覽器輸出的格式為xml,不然瀏覽器顯示不出xml的格式require_once(‘sitemap_data.php‘); //把資料來源載入進來$sitemap=$sitemap; //這裡要按照sitemap的格式構造出xml的檔案,urlset url loc是規定必須有的標籤$xml_wrapper = <<<XML<?xml version=‘1.0‘ encoding=‘utf-8‘?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>XML;//$xml = simplexml_load_string($xml_wrapper);$xml = new SimpleXMLElement($xml_wrapper);foreach ($sitemap as $data) { $item = $xml->addChild(‘url‘); //使用addChild添加節點 if (is_array($data)) { foreach ($data as $key => $row) { $node = $item->addChild($key, $row); if (isset($attribute_array[$key]) && is_array($attribute_array[$key])) { foreach ($attribute_array[$key] as $akey => $aval) {//設定屬性值,我這裡為空白 $node->addAttribute($akey, $aval); } } } }}echo $xml->asXML(); //用asXML方法輸出xml,預設只構造不輸出。?>
另外網上也找到其他方法比如DOMDocument來構造xml,但通過比較使用simpleXML類是最省代碼,實現起來也很簡單。
其他方法可以參考這文章
http://www.phppan.com/2009/10/use-php-create-xml-file/
本文出自 “安家圈子交流學習” 部落格,請務必保留此出處http://brucetam.blog.51cto.com/1863614/1573444
php 產生xml檔案