分享一个生成sitemap.xml的类

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

很多站长在做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解析html

注:分析html的好东西 最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的...

如何实现PHP的计划(定时)任务和暂停任务

如果在服务器做计划任务是件很简单的事情,但是有的时候收条件限制无法使用服务器自带的计划任务,比如买的空间,这时候想要让网站定时执行某些操作(处理数据、生成静态文件、清除缓存...),该怎么办呢? &...

一些需要禁用的PHP危险函数

phpinfo() 功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。 危险等级:中 passthru() 功能描述:允许执行一个外部程序并回显输出,类似于 exec()。...

emlog博客系统有些服务器获取不到正确的客户IP解决办法

有些情况下,emlog博客系统自带的获取IP的函数无法获取到正确的客户端IP(本站就是),比如站点启用CDN服务器之后emlog就会无法正确的获取到访客的IP地址, 解决这个问题我们需要修改一下eml...

可以提高PHP编程效率的一些方法

1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。 2、$row[’id’] 的速度是$row[id]的7倍。 3、echo 比 print 快,并且使用e...

PHP超级简单的把数据库导出Excel表格方法

这段时间一直为手头上的事情忙碌着,以至于很少搭理这个博客。刚刚开了一下这个博客的使用时间,在2012年11月份,到现在,已经整整两年了。还好用的是自己的服务器,不然这些日子的忙碌无暇顾及博客,早就被空...

发表评论

访客

看不清,换一张

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