php实现的RSS生成类实例
程序员文章站
2023-09-02 18:44:46
本文实例讲述了php实现的rss生成类。分享给大家供大家参考。具体如下:
class rss
{
var $title;
var $link;
va...
本文实例讲述了php实现的rss生成类。分享给大家供大家参考。具体如下:
class rss { var $title; var $link; var $description; var $language = "en-us"; var $pubdate; var $items; var $tags; function rss() { $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 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"; 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>'; } } class rssitem { var $title; var $link; var $description; var $pubdate; var $guid; var $tags; var $attachment; var $length; var $mimetype; function rssitem() { $this->tags = array(); } 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 .= "<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($this->attachment != "") $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />"; 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; } }
使用示例如下:
$feed = new rss(); $feed->title = "rss feed title"; $feed->link = "http://website.com"; $feed->description = "recent articles on your website."; $db->query($query); $result = $db->result; while($row = mysql_fetch_array($result, mysql_assoc)) { $item = new rssitem(); $item->title = $title; $item->link = $link; $item->setpubdate($create_date); $item->description = "<![cdata[ $html ]]>"; $feed->additem($item); } echo $feed->serve();
希望本文所述对大家的php程序设计有所帮助。
上一篇: jQuery实现table表格信息的展开和缩小功能示例
下一篇: 便秘怎么调理 12种饮食疗法帮助你