<?php |
002 | class RSS |
003 | { |
004 | /** |
005 | +---------------------------------------------------------- |
006 | * RSS频道名 |
007 | +---------------------------------------------------------- |
008 | */ |
009 | protected $channel_title = '' ; |
010 | /** |
011 | +---------------------------------------------------------- |
012 | * RSS频道链接 |
013 | +---------------------------------------------------------- |
014 | */ |
015 | protected $channel_link = '' ; |
016 | /** |
017 | +---------------------------------------------------------- |
018 | * RSS频道描述 |
019 | +---------------------------------------------------------- |
020 | */ |
021 | protected $channel_description = '' ; |
022 | /** |
023 | +---------------------------------------------------------- |
024 | * RSS频道使用的小图标的URL |
025 | +---------------------------------------------------------- |
026 | */ |
027 | protected $channel_imgurl = '' ; |
028 | /** |
029 | +---------------------------------------------------------- |
030 | * RSS频道所使用的语言 |
031 | +---------------------------------------------------------- |
032 | */ |
033 | protected $language = 'zh_CN' ; |
034 | /** |
035 | +---------------------------------------------------------- |
036 | * RSS文档创建日期,默认为今天 |
037 | +---------------------------------------------------------- |
038 | */ |
039 | protected $pubDate = '' ; |
040 | protected $lastBuildDate = '' ; |
041 | |
042 | protected $generator = 'YBlog RSS Generator' ; |
043 | |
044 | /** |
045 | +---------------------------------------------------------- |
046 | * RSS单条信息的数组 |
047 | +---------------------------------------------------------- |
048 | */ |
049 | protected $items = array (); |
050 | |
051 | /** |
052 | +---------------------------------------------------------- |
053 | * 构造函数 |
054 | +---------------------------------------------------------- |
055 | * @access public |
056 | +---------------------------------------------------------- |
057 | * @param string $title RSS频道名 |
058 | * @param string $link RSS频道链接 |
059 | * @param string $description RSS频道描述 |
060 | * @param string $imgurl RSS频道图标 |
061 | +---------------------------------------------------------- |
062 | */ |
063 | public function __construct( $title , $link , $description , $imgurl = '' ) |
064 | { |
065 | $this ->channel_title = $title ; |
066 | $this ->channel_link = $link ; |
067 | $this ->channel_description = $description ; |
068 | $this ->channel_imgurl = $imgurl ; |
069 | $this ->pubDate = Date ( 'Y-m-d H:i:s' , time()); |
070 | $this ->lastBuildDate = Date ( 'Y-m-d H:i:s' , time()); |
071 | } |
072 | |
073 | /** |
074 | +---------------------------------------------------------- |
075 | * 设置私有变量 |
076 | +---------------------------------------------------------- |
077 | * @access public |
078 | +---------------------------------------------------------- |
079 | * @param string $key 变量名 |
080 | * @param string $value 变量的值 |
081 | +---------------------------------------------------------- |
082 | */ |
083 | public function Config( $key , $value ) |
084 | { |
085 | $this ->{ $key } = $value ; |
086 | } |
087 | |
088 | /** |
089 | +---------------------------------------------------------- |
090 | * 添加RSS项 |
091 | +---------------------------------------------------------- |
092 | * @access public |
093 | +---------------------------------------------------------- |
094 | * @param string $title 日志的标题 |
095 | * @param string $link 日志的链接 |
096 | * @param string $description 日志的摘要 |
097 | * @param string $pubDate 日志的发布日期 |
098 | +---------------------------------------------------------- |
099 | */ |
100 | function AddItem( $title , $link , $description , $pubDate ) |
101 | { |
102 | $this ->items[] = array ( 'title' => $title , 'link' => $link , 'description' => $description , 'pubDate' => $pubDate ); |
103 | } |
104 | |
105 | /** |
106 | +---------------------------------------------------------- |
107 | * 输出RSS的XML为字符串 |
108 | +---------------------------------------------------------- |
109 | * @access public |
110 | +---------------------------------------------------------- |
111 | * @return string |
112 | +---------------------------------------------------------- |
113 | */ |
114 | public function Fetch() |
115 | { |
116 | $rss = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" ; |
117 | $rss = "<rss version=\"2.0\">\r\n" ; |
118 | $rss .= "<channel>\r\n" ; |
119 | $rss .= "<title><![CDATA[{$this->channel_title}]]></title>\r\n" ; |
120 | $rss .= "<description><![CDATA[{$this->channel_description}]]></description>\r\n" ; |
121 | $rss .= "<link>{$this->channel_link}</link>\r\n" ; |
122 | $rss .= "<language>{$this->language}</language>\r\n" ; |
123 | |
124 | if (! empty ( $this ->pubDate)) |
125 | $rss .= "<pubDate>{$this->pubDate}</pubDate>\r\n" ; |
126 | if (! empty ( $this ->lastBuildDate)) |
127 | $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\r\n" ; |
128 | if (! empty ( $this ->generator)) |
129 | $rss .= "<generator>{$this->generator}</generator>\r\n" ; |
130 | |
131 | $rss .= "<ttl>5</ttl>\r\n" ; |
132 | |
133 | if (! empty ( $this ->channel_imgurl)) { |
134 | $rss .= "<image>\r\n" ; |
135 | $rss .= "<title><![CDATA[{$this->channel_title}]]></title>\r\n" ; |
136 | $rss .= "<link>{$this->channel_link}</link>\r\n" ; |
137 | $rss .= "<url>{$this->channel_imgurl}</url>\r\n" ; |
138 | $rss .= "</image>\r\n" ; |
139 | } |
140 | |
141 | for ( $i = 0; $i < count ( $this ->items); $i ++) { |
142 | $rss .= "<item>\r\n" ; |
143 | $rss .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\r\n" ; |
144 | $rss .= "<link>{$this->items[$i]['link']}</link>\r\n" ; |
145 | $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\r\n" ; |
146 | $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\r\n" ; |
147 | $rss .= "</item>\r\n" ; |
148 | } |
149 | |
150 | $rss .= "</channel>\r\n</rss>" ; |
151 | return $rss ; |
152 | } |
153 | |
154 | /** |
155 | +---------------------------------------------------------- |
156 | * 输出RSS的XML到浏览器 |
157 | +---------------------------------------------------------- |
158 | * @access public |
159 | +---------------------------------------------------------- |
160 | * @return void |
161 | +---------------------------------------------------------- |
162 | */ |
163 | public function Display() |
164 | { |
165 | header( "Content-Type: text/xml; charset=utf-8" ); |
166 | echo $this ->Fetch(); |
167 | exit ; |
168 | } |
169 | } |
170 | ?> |