PHP implementation of the RSS generation class instance _php skills

Source: Internet
Author: User

The examples in this article describe the RSS-generated classes for PHP implementations. Share to everyone for your reference. as follows:

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; The 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;
  The function serve ($contentType = "Application/xml") {$xml = $this->out ();
  Header ("Content-type: $contentType");
 Echo $xml; The 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; }
}

Use the example below:

$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 ();

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.