这篇文章主要介绍了php实现的rss生成类,实例分析了rss生成类的原理、定义与使用技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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; elsereturn $this->pubdate; } function addtag($tag, $value) { $this->tags[$tag] = $value; } 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 $key => $val) $out .= $val$key>\n; foreach($this->items as $item) $out .= $item->out(); $out .= \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 = '' . \n; $out .= '' . \n; return $out; } function footer() { return ''; }}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; elsereturn $this->pubdate; } function addtag($tag, $value) { $this->tags[$tag] = $value; } function out() { $out .= \n; $out .= . $this->title . \n; $out .= . $this->link . \n; $out .= . $this->description . \n; $out .= . $this->getpubdate() . \n; if($this->attachment != )$out .= attachment} length={$this->length} type={$this->mimetype}>; if(empty($this->guid)) $this->guid = $this->link; $out .= . $this->guid . \n;foreach($this->tags as $key => $val) $out .= $val$key\n>; $out .= \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 = ; $feed->additem($item);}echo $feed->serve();
希望本文所述对大家的php程序设计有所帮助。
,