温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php 生成sitemap xml文件(网站地图)

发布时间:2020-07-13 13:40:32 来源:网络 阅读:5594 作者:brucetam322 栏目:web开发

最近帮朋友优化一个网站,想生成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/

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI