好多都是第三方的,自己闲麻烦,自己就整理了一套自己一个rss类,对需要的朋友给个帮助吧,也方便了自己,呵呵

直接贴代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
class Rss extends CController
{
//public
public $rss_ver = "2.0";
public $channel_title = '';
public $channel_link = '';
public $channel_description = '';
public $language = 'zh_CN';
public $copyright = '';
public $webMaster = '';
public $pubDate = '';
public $lastBuildDate = '';
public $generator = 'GoWhich RSS Generator';
public $content = '';
public $items = [];

/**
* 添加基本信息
* @param string $title
* @param string $link
* @param string $description
*/
public function __construct($title, $link, $description)
{
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubDate = Date('Y-m-d H:i:s', time());
$this->lastBuildDate = Date('Y-m-d H:i:s', time());
}

/**
* 添加一个节点
* @param string $title
* @param string $link
* @param string $description
* @param date $pubDate
*/
public function addItem($title, $link, $description, $pubDate)
{
$this->items[] = ['title' => $title,
'link' => $link,
'descrīption' => $description,
'pubDate' => $pubDate];
}

/**
* 构建xml元素
*/
public function buildRSS()
{
$s = <<<RSS
<?xml version='1.0' encoding='utf-8'?>\n
<rss version="2.0">\n
RSS;

// start channel
$s .= "<channel>\n";
$s .= "<title><![CDATA[{$this->channel_title}]]></title>\n";
$s .= "<link><![CDATA[{$this->channel_link}]]></link>\n";
$s .= "<descrīption><![CDATA[{$this->channel_description}]]></descrīption>\n";
$s .= "<language>{$this->language}</language>\n";
if (!empty($this->copyright)) {
$s .= "<copyright><![CDATA[{$this->copyright}]]></copyright>\n";
}
if (!empty($this->webMaster)) {
$s .= "<webMaster><![CDATA[{$this->webMaster}]]></webMaster>\n";
}
if (!empty($this->pubDate)) {
$s .= "<pubDate>{$this->pubDate}</pubDate>\n";
}
if (!empty($this->lastBuildDate)) {
$s .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
}
if (!empty($this->generator)) {
$s .= "<generator>{$this->generator}</generator>\n";
}
// start items
for ($i = 0; $i < count($this->items); $i++) {
$s .= "<item>\n";
$s .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\n";
$s .= "<link><![CDATA[{$this->items[$i]['link']}]]></link>\n";
$s .= "<descrīption><![CDATA[{$this->items[$i]['descrīption']}]]></descrīption>\n";
$s .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";
$s .= "</item>\n";
}
// close channel
$s .= "</channel>\n</rss>";
$this->content = $s;
}

/**
* 输出rss内容
*/
public function show()
{
if (empty($this->content)) {
$this->buildRSS();
}
return $this->content;
}

/**
* 设置版权
* @param unknown $copyright
*/
public function setCopyRight($copyright)
{
$this->copyright = $copyright;
}

/**
* 设置管理员
* @param unknown $master
*/
public function setWebMaster($master)
{
$this->webMaster = $master;
}

/**
* 设置发布时间
* @param date $date
*/
public function setpubDate($date)
{
$this->pubDate = $date;
}

/**
* 设置建立时间
* @param unknown $date
*/
public function setLastBuildDate($date)
{
$this->lastBuildDate = $date;
}

/**
* 将rss保存为文件
* @param String $fname
* @return boolean
*/
public function saveToFile($fname)
{
$handle = fopen($fname, 'wb');
if (false === $handle) {
return false;
}
fwrite($handle, $this->content);
fclose($handle);
}

/**
* 获取文件的内容
* @param String $fname
* @return boolean
*/
public function getFile($fname)
{
$handle = fopen($fname, 'r');
if (false === $handle) {
return false;
}
while (!feof($handle)) {
echo fgets($handle);
}
fclose($handle);
}
}

如果还是不都清楚可以到github上自己下载好了

github地址:https://github.com/zhangda89/php-library/blob/master/Rss.php