分享一个生成sitemap.xml的类

飘逸的风14年前 (2012-11-19)程序6827

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

相关文章

php日历的高效写法

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

如何在 SAE 下操作本地IO

因为SAE平台安全性的考虑,限制了用户对于本地IO的使用,但这样对于一些传统的PHP项目,也许带来了很多不便,因为它们都或多或少的有对本地IO的操作,像Smarty的编译模板和文件上传程序。为了解决这...

新的月份,换一个新的模板

新的月份,换一个新的模板

用了半天的时间做一个emlog模板,自己感觉还是不错的。emlog模板做起来还是很简单的!...

PHP攻击网站防御代码-以及攻击代码反译

这是我无意中攻击一个网站发现的一个代码PHP有效的拦截住我的DDOS  <?php   //查询禁止IP   $ip =$_SERVER['R...

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

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

龙圆 php 判断是否为搜索引擎蜘蛛函数 转载

/**  *    判断是否为搜索引擎蜘蛛  *  *    @author&nbs...

发表评论

访客

看不清,换一张

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