分享一个生成sitemap.xml的类
很多站长在做seo优化的时候都会向各大搜索引擎网站提交站点地图sitemap.xml,同样需要提交。于是专门写了一个生成sitemap.xml的类,支持生成在制定目录下,默认生成在网站根目录下。
分享下代码sitemap.class.php:
001 |
<?php |
002 |
003 |
/** |
004 |
+------------------------------------------------------------------------------ |
005 |
* SITEMAP生成类 |
006 |
+------------------------------------------------------------------------------ |
007 |
* author:leo.li |
008 |
* QQ:281978297 |
009 |
* email:281978297@qq.com |
010 |
+------------------------------------------------------------------------------ |
011 |
$stitmap = new sitemap("sitemap.xml"); |
012 |
$stitmap->AddItem("https://licao.so","2012-03-27T09:04:04-08:00","always","0.8"); |
013 |
$stitmap->Display(TRUE); |
014 |
* |
015 |
+------------------------------------------------------------------------------ |
016 |
*/ |
017 |
class sitemap { |
018 |
019 |
protected $items = array(); |
020 |
protected $dir; //生成sitemap目录路径 |
021 |
protected $xmlName; //生成sitemap文件名 |
022 |
023 |
public function __construct($xmlName, $dir) { |
024 |
$this->dir = $dir; |
025 |
$this->xmlName = $xmlName; |
026 |
} |
027 |
028 |
/** |
029 |
+---------------------------------------------------------- |
030 |
* 添加siteUrl项 |
031 |
+---------------------------------------------------------- |
032 |
* @param string $loc 链接 |
033 |
* @param string $lastmod 更新时间 |
034 |
* @param string $changefreq 更新频率 "always", "hourly", "daily", "weekly", "monthly", "yearly" |
035 |
* @param string $priority 级别 |
036 |
+---------------------------------------------------------- |
037 |
*/ |
038 |
function AddItem($loc, $changefreq, $lastmod, $priority) { |
039 |
$lastmod = empty($lastmod) ? date("Y-m-d") . "T" . date("H:i:s-08:00") : $lastmod; |
040 |
$changefreq = empty($changefreq) ? "always" : $changefreq; |
041 |
$priority = empty($priority) ? "0.8" : $priority; |
042 |
$this->items[] = array('loc' => $loc, 'lastmod' => $lastmod, 'changefreq' => $changefreq, 'priority' => $priority); |
043 |
} |
044 |
045 |
/** |
046 |
+---------------------------------------------------------- |
047 |
* 输出SITEMAP的XML为字符串 |
048 |
+---------------------------------------------------------- |
049 |
* @return string |
050 |
+---------------------------------------------------------- |
051 |
*/ |
052 |
public function Fetch() { |
053 |
$map = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n"; |
054 |
$map.="<urlset xmlns=\"https://www.sitemaps.org/schemas/sitemap/0.9\">\r\n"; |
055 |
foreach ($this->items as $v) { |
056 |
$map.="\t<url>\r\n"; |
057 |
$map.="\t\t<loc>" . $v[loc] . "</loc>\r\n"; |
058 |
$map.="\t\t<lastmod>" . $v[lastmod] . "</lastmod>\r\n"; |
059 |
$map.="\t\t<changefreq>" . $v[changefreq] . "</changefreq>\r\n"; |
060 |
$map.="\t\t<priority>" . $v[priority] . "</priority>\r\n"; |
061 |
$map.="\t</url>\r\n"; |
062 |
} |
063 |
$map.='</urlset>'; |
064 |
return $map; |
065 |
} |
066 |
067 |
/** |
068 |
+---------------------------------------------------------- |
069 |
* 输出SITEMAP的XML到浏览器 |
070 |
+---------------------------------------------------------- |
071 |
* @param boole $display 是否显示sitemap内容 |
072 |
+---------------------------------------------------------- |
073 |
*/ |
074 |
public function Display($display=FALSE) { |
075 |
$xml = $this->Fetch(); |
076 |
if ($display == TRUE) { |
077 |
header("Content-Type: text/xml; charset=utf-8"); |
078 |
echo $xml; |
079 |
} |
080 |
$this->makeXml($this->dir, $this->xmlName, $xml, $display); |
081 |
} |
082 |
083 |
/** |
084 |
+---------------------------------------------------------- |
085 |
* 检测目录,不存在创建目录 |
086 |
+---------------------------------------------------------- |
087 |
* @param str $dir 目录 |
088 |
+---------------------------------------------------------- |
089 |
*/ |
090 |
function make_dir($dir) { |
091 |
return is_dir($dir) or (make_dir(dirname($dir)) and @mkdir($dir, 0777)); |
092 |
} |
093 |
094 |
/** |
095 |
+---------------------------------------------------------- |
096 |
* 生成sitemap.xml文件 |
097 |
+---------------------------------------------------------- |
098 |
* @param str $dir 目录 |
099 |
* @param str $xml sitemap文件名 |
100 |
* @param str $xml sitemap内容 |
101 |
* @param str $display 是否在页面中显示sitemap内容 |
102 |
+---------------------------------------------------------- |
103 |
*/ |
104 |
function makeXml($dir, $xmlName, $xml, $display) { |
105 |
$dir = $dir == '' ? $_SERVER[DOCUMENT_ROOT] : $this->make_dir($dir); |
106 |
$xmlName = $xmlName == '' ? $dir . "sitemap.xml" : $dir . $xmlName; |
107 |
$handle = fopen($xmlName, "w"); |
108 |
if (!fwrite($handle, $xml)) { |
109 |
if ($display == FALSE) { |
110 |
die("生成文件" . $xmlName . "失败!"); |
111 |
} |
112 |
} else { |
113 |
if ($display == FALSE) { |
114 |
die("生成文件" . $xmlName . "成功!"); |
115 |
} |
116 |
} |
117 |
fclose($handle); |
118 |
} |
119 |
120 |
} |
121 |
122 |
?> |
123 |
124 |
用法:载入sitemap.class.php后:<pre class="brush:php; toolbar: true; auto-links: true;"> $stitmap = new sitemap(); |
125 |
$stitmap->AddItem("https://licao.so","2012-03-27T09:04:04-08:00","always","0.8"); |
126 |
$stitmap->Display(TRUE);</pre><br> |
1 |
$stitmap = new sitemap("sitemap.xml"); |
2 |
$stitmap->AddItem("https://licao.so","2012-03-27T09:04:04-08:00","always","0.8"); |
3 |
$stitmap->Display(TRUE); |
1 |
$stitmap = new sitemap("sitemap.xml","./sitemap/"); |
2 |
$stitmap->AddItem("https://licao.so","2012-03-27T09:04:04-08:00","always","0.8"); |
3 |
$stitmap->Display(TRUE); |

![emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]](/content/uploadfile/201211/thum-11671353411977.jpg)