分享一个生成sitemap.xml的类

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

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

相关文章

38条PHP编码优化加速技巧[转]

1. 尽量采用大量的PHP内置函数。 2. echo 比 print 快。 3. 不要把方法细分得过多,仔细想想你真正打算重用的是哪些代码? 4. 在执行for循环之前确...

PHP给图片添加水印和生成缩略图函数

给图片添加水印: /************************************ 函数: watermark($bigimg, $smallimg, $coord = 1) 作用:...

PHP 文件上传$_FILES

文件上传表单<form enctype="multipart/form-data" action="URL" method="post">  <inp...

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

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

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

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

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

分享:PHP获取MAC地址的类库

PHP获取MAC地址的类库代码: <?php /** 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址...

发表评论

访客

看不清,换一张

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