当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现的RSS生成类实例

php实现的RSS生成类实例

2018年06月23日  | 移动技术网IT编程  | 我要评论

糟饼阅读答案,试题库管理系统,钓鱼岛地理位置

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网