ThinkPHP 生成 RSS
以下包含了两个类: RSS 和 RSSItem。(推荐存放在 ./extends/rss
目录下)
创建一个新的 RSS 对象,然后用 RSSItems 填充它。支持多媒体(音频/视频文件)。
RSS类:
<?php
namespace rss;
class RSS
{
var $title;
var $link;
var $description;
var $language = "en-us";
var $pubDate;
var $items;
var $tags;
var $lastBuildDate;
var $image;
var $managingEditor;
var $ttl;
var $webMaster;
var $copyright;
function __construct()
{
$this->items = array();
$this->tags = array();
}
function addItem($item)
{
$this->items[] = $item;
}
function setPubDate($when)
{
if(strtotime($when) == false)
$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
else
$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
}
function getPubDate()
{
if(empty($this->pubDate))
return date("D, d M Y H:i:s ") . "GMT";
else
return $this->pubDate;
}
function addTag($tag, $value)
{
$this->tags[$tag] = $value;
}
function setLastBuildDate($when)
{
if(strtotime($when) == false)
$this->lastBuildDate = date("D, d M Y H:i:s ", $when) . "GMT";
else
$this->lastBuildDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
}
function getLastBuildDate()
{
if(empty($this->lastBuildDate))
return date("D, d M Y H:i:s ") . "GMT";
else
return $this->lastBuildDate;
}
function setImage($value)
{
$this->image = $value;
}
function setManagingEditor($value)
{
$this->managingEditor = $value;
}
function setTTL($value)
{
$this->ttl = $value;
}
function setWebMaster($value)
{
$this->webMaster = $value;
}
function setCopyright($value)
{
$this->copyright = $value;
}
function out()
{
$out = $this->header();
$out .= "<channel>\n";
$out .= "<title>" . $this->title . "</title>\n";
$out .= "<link>" . $this->link . "</link>\n";
$out .= "<description>" . $this->description . "</description>\n";
$out .= "<language>" . $this->language . "</language>\n";
$out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
$out .= "<lastBuildDate>" . $this->getLastBuildDate() . "</lastBuildDate>\n";
if (!empty($this->image)) {
$out .= "<image>\n";
$out .= "<url>" . $this->image . "</url>";
$out .= "<title>" . $this->title . "</title>";
$out .= "<link>" . $this->link . "</link>";
$out .= "</image>\n";
}
if (!empty($this->managingEditor)) {
$out .= "<managingEditor>" . $this->managingEditor . "</managingEditor>\n";
}
if (!empty($this->ttl)) {
$out .= "<ttl>" . $this->ttl . "</ttl>\n";
}
if (!empty($this->webMaster)) {
$out .= "<webMaster>" . $this->webMaster . "</webMaster>\n";
}
if (!empty($this->copyright)) {
$out .= "<copyright>" . $this->copyright . "</copyright>\n";
}
foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
foreach($this->items as $item) $out .= $item->out();
$out .= "</channel>\n";
$out .= $this->footer();
$out = str_replace("&", "&", $out);
return $out;
}
function serve($contentType = "application/xml")
{
$xml = $this->out();
header("Content-type: $contentType");
echo $xml;
}
function header()
{
$out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
return $out;
}
function footer()
{
return '</rss>';
}
}
RSSItem类:
<?php
namespace rss;
class RSSItem
{
var $title;
var $link;
var $description;
var $pubDate;
var $guid;
var $tags;
var $attachment;
var $length;
var $mimetype;
var $author;
var $category;
var $comments;
var $source;
function __construct()
{
$this->tags = array();
$this->enclosure = array();
}
function setSource($value)
{
$this->source = $value;
}
function setComments($value)
{
$this->comments = $value;
}
function setCategory($value)
{
$this->category = $value;
}
function setAuthor($value)
{
$this->author = $value;
}
function setPubDate($when)
{
if(strtotime($when) == false)
$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
else
$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
}
function getPubDate()
{
if(empty($this->pubDate))
return date("D, d M Y H:i:s ") . "GMT";
else
return $this->pubDate;
}
function addTag($tag, $value)
{
$this->tags[$tag] = $value;
}
function out()
{
$out = '';
$out .= "<item>\n";
$out .= "<title>" . $this->title . "</title>\n";
$out .= "<link>" . $this->link . "</link>\n";
$out .= "<description>" . $this->description . "</description>\n";
$out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
if (!empty($this->author)) {
$out .= "<author>" . $this->author . "</author>\n";
}
if (!empty($this->category)) {
$out .= "<category>" . $this->category . "</category>\n";
}
if (!empty($this->comments)) {
$out .= "<comments>" . $this->comments . "</comments>\n";
}
if (!empty($this->source)) {
$out .= "<source>" . $this->source . "</source>\n";
}
if($this->attachment != "")
$out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />\n";
if(empty($this->guid)) $this->guid = $this->link;
$out .= "<guid>" . $this->guid . "</guid>\n";
foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
$out .= "</item>\n";
return $out;
}
function enclosure($url, $mimetype, $length)
{
$this->attachment = $url;
$this->mimetype = $mimetype;
$this->length = $length;
}
}
实例:
$article = Article::field([
'*',
])->order([
'm_time' => 'desc',
'id' => 'desc',
])->limit(20)->select()->toArray();
$feed = new RSS();
$feed->title = $this->config['site_title'];
$feed->link = request()->domain();
$feed->description = $this->config['meta_description'];
$feed->language = 'zh-cn';
$feed->setWebMaster('1540294142@qq.com');
foreach ($article as $item) {
$rssitem = new RSSItem();
$rssitem->title = $item['title'];
$rssitem->link = request()->domain() . url('read', ['id' => $item['id']]);
$rssitem->setPubDate($item['c_time']);
$rssitem->description = '.$item['desc'].' ]]>';
$rssitem->setAuthor('1540294142@qq.com');
$feed->addItem($rssitem);
}
return xml($feed->out());