最近幫朋友優化一個網站,想生成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文件
<?php header('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/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。