RSS 類別結構
class RSS {
public $title, $link, $description, $language = 'en-us', $pubDate;
private $items = [], $tags = [];
public function addItem(RSSItem $item) { $this->items[] = $item; }
public function setPubDate($when) { /* strtotime 處理 */ }
public function getPubDate() { /* 回傳 RFC 2822 格式 */ }
public function addTag($tag, $value) { $this->tags[$tag] = $value; }
public function out() {
$out = $this->header();
$out .= "\n";
$out .= "{$this->title}\n";
$out .= "{$this->link}\n";
$out .= "{$this->description}\n";
$out .= "{$this->language}\n";
$out .= "{$this->getPubDate()}\n";
foreach ($this->tags as $k => $v) $out .= "<$k>$v$k>\n";
foreach ($this->items as $item) $out .= $item->out();
$out .= "\n";
$out .= $this->footer();
return str_replace('&', '&', $out);
}
public function serve($contentType = 'application/xml') {
header("Content-type: $contentType");
echo $this->out();
}
private function header() { return '' . "\n\n"; }
private function footer() { return ''; }
}
class RSSItem {
public $title, $link, $description, $pubDate, $guid, $tags = [];
public $attachment, $length, $mimetype;
public function setPubDate($when) { /* ... */ }
public function getPubDate() { /* ... */ }
public function addTag($tag, $value) { $this->tags[$tag] = $value; }
public function enclosure($url, $mimetype, $length) { /* iTunes Podcast 支援 */ }
public function out() {
$out = "- \n";
$out .= "{$this->title}\n";
$out .= "{$this->link}\n";
$out .= "{$this->description}\n";
$out .= "{$this->getPubDate()}\n";
if ($this->attachment) $out .= "\n";
$out .= "{$this->guid ?: $this->link}\n";
foreach ($this->tags as $k => $v) $out .= "<$k>$v$k>\n";
$out .= "
\n";
return $out;
}
}
使用範例
$rss = new RSS();
$rss->title = '我的部落格';
$rss->link = 'https://blog.example.com';
$rss->description = '分享技術心得';
$rss->setPubDate(time());
$item = new RSSItem();
$item->title = 'PHP 8.1 新特性';
$item->link = 'https://blog.example.com/php81';
$item->description = 'Enumerations, Fibers, never return type...';
$item->setPubDate(strtotime('-1 day'));
$item->addTag('dc:creator', 'John Doe');
$rss->addItem($item);
// 直接輸出
$rss->serve();
// 或取得字串
$xml = $rss->out();
file_put_contents('feed.xml', $xml);
輸出 XML 範例
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="https://purl.org/dc/elements/1.1/">
<channel>
<title>我的部落格</title>
<link>https://blog.example.com</link>
<description>分享技術心得</description>
<language>en-us</language>
<pubDate>Mon, 01 Jan 2024 00:00:00 GMT</pubDate>
<item>
<title>PHP 8.1 新特性</title>
<link>https://blog.example.com/php81</link>
<description>Enumerations, Fibers, never return type...</description>
<pubDate>Sun, 31 Dec 2023 00:00:00 GMT</pubDate>
<guid>https://blog.example.com/php81</guid>
<dc:creator>John Doe</dc:creator>
</item>
</channel>
</rss>
現代替代:使用套件
// composer require suin/rss-writer
use Suin\RSSWriter\Feed;
use Suin\RSSWriter\Item;
$feed = new Feed();
$feed->title = '...';
$feed->link = '...';
$feed->description = '...';
$item = new Item();
$item->title = '...';
$item->link = '...';
$item->description = '...';
$item->date = time();
$feed->addItem($item);
echo $feed->render();