分享一个生成sitemap.xml的类

飘逸的风13年前 (2012-11-19)程序6595

很多站长在做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($diror (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);
文章来源:https://blog.51edm.org/post/44

相关文章

如何修改PHP的memory_limit限制

如何修改PHP的memory_limit限制

在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了过多的内存,并超...

文章内链的实现方法

在很多的时候,我在浏览其他的网站发现文章中一写关键词都被加上了链接链向对应的页面,我就在想他们是怎么实现这个功能的?为什么这么做,这么做有什么好处?   当然有什么好处一看便知,无论是从...

MemCache安装使用

Windows下的Memcache安装: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c:\me...

php日历的高效写法

标题写的吸引人了些,不过我就觉得是挺高效的,我相信 之前看到过 irlvirus 写的 《php练习代码-日历》,我觉得效率都不是很好。 date()函数结合mktime() 可以得到一切东西...

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件,可以把代码贴在代码框中,确定后自动生成高亮代码。 注:本插件已升级,修复在代码很长换行的时候左边的行数不增加问题;插入代码里有HTML标...

thinkphp 使用U方法自动生成URL超链接

ThinkPHP U方法 U 方法是 ThinkPHP 内置的一个快捷方法,可以根据系统 URL 模式配置动态的生成智能的 URL 地址。 由于 ThinkPHP 支持各种不同的 URL...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。