外热呃 发表于 2014-11-7 08:55:21

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

最近帮朋友优化一个网站,想生成xml格式的sitemap然后提交给搜索引擎,利用php的simpleXML类就很容易实现了。贴一下代码块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//sitemap_data.php 包含了网站所有链接的信息,直接贴出输出的数据,源码就不贴了
array(22) {
=>
array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/index.php"
}
=>
array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/about.php"
}
=>
array(1) {
    ["loc"]=>
    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=1"
}
=>
array(1) {
    ["loc"]=>
    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=2"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=1"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=2"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=3"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=4"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=5"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=6"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=7"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=8"
}
=>
array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=9"
}
=>
array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/order.php"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=1"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=2"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=3"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=4"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=5"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=6"
}
=>
array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=7"
}
=>
array(1) {
    ["loc"]=>
    string(34) "http://www.ibxg.com.cn/contact.php"
}
}





sitemap_xml.php文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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类是最省代码,实现起来也很简单。

页: [1]
查看完整版本: php 生成sitemap xml文件(网站地图)