简单的php生成rss的 类
1 <?php
2 if (defined ( '_CLASS_RSS_PHP' )) return;
3 define ( '_CLASS_RSS_PHP', 1 );
4 /**
5 * RSS
6 * @author sunny5156
7 *
8 */
9 class RSS {
10 // public
11 var $rss_ver = "2.0";
12 var $channel_title = '';
13 var $channel_link = '';
14 var $channel_description = '';
15 var $language = 'zh_CN';
16 var $copyright = '';
17 var $webMaster = '';
18 var $pubDate = '';
19 var $lastBuildDate = '';
20 var $generator = 'RedFox RSS Generator';
21 var $content = '';
22 var $items = array ();
23
24 /**
25 * 构造RSS
26 *
27 * @param string $title
28 * @param string $link
29 * @param string $description
30 */
31 function RSS($title, $link, $description) {
32 $this->channel_title = $title;
33 $this->channel_link = $link;
34 $this->channel_description = $description;
35 $this->pubDate = Date ( 'Y-m-d H:i:s', time () );
36 $this->lastBuildDate = Date ( 'Y-m-d H:i:s', time () );
37 }
38
39 /**
40 * 添加节点
41 *
42 * @param string $title
43 * @param string $link
44 * @param string $description
45 * @param string $pubDate
46 */
47 function AddItem($title, $link, $description, $pubDate) {
48 $this->items [] = array (
49 'title' => $title,
50 'link' => $link,
51 'description' => $description,
52 'pubDate' => $pubDate
53 );
54 }
55
56 /**
57 * 生成RSS
58 */
59 function BuildRSS() {
60 $s = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\">";
61 // start channel
62 $s .= "<channel>\n";
63 $s .= "<title><![CDATA[{$this->channel_title}]]></title>\n";
64 $s .= "<link><![CDATA[{$this->channel_link}]]></link>\n";
65 $s .= "<description><![CDATA[{$this->channel_description}]]></description>\n";
66 $s .= "<language>{$this->language}</language>\n";
67 if (! empty ( $this->copyright )) {
68 $s .= "<copyright><![CDATA[{$this->copyright}]]></copyright>\n";
69 }
70 if (! empty ( $this->webMaster )) {
71 $s .= "<webMaster><![CDATA[{$this->webMaster}]]></webMaster>\n";
72 }
73 if (! empty ( $this->pubDate )) {
74 $s .= "<pubDate>{$this->pubDate}</pubDate>\n";
75 }
76
77 if (! empty ( $this->lastBuildDate )) {
78 $s .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
79 }
80
81 if (! empty ( $this->generator )) {
82 $s .= "<generator>{$this->generator}</generator>\n";
83 }
84
85 // start items
86 for($i = 0; $i < count ( $this->items ); $i ++) {
87 $s .= "<item>\n";
88 $s .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\n";
89 $s .= "<link><![CDATA[{$this->items[$i]['link']}]]></link>\n";
90 $s .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\n";
91 $s .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";
92 $s .= "</item>\n";
93 }
94
95 // close channel
96 $s .= "</channel>\n</rss>";
97 $this->content = $s;
98 }
99
100 /**
101 * 显示RSS
102 */
103 function Show() {
104 if (empty ( $this->content ))
105 $this->BuildRSS ();
106 print_r ($this->content);
107 }
108
109 /**
110 * 生成文件
111 * @param string $fname
112 * @return boolean
113 */
114 function SaveToFile($fname) {
115 $handle = fopen ( $fname, 'wb' );
116 if ($handle === false)
117 return false;
118 fwrite ( $handle, $this->content );
119 fclose ( $handle );
120 }
121 }
122 ?>