PHP implementation of the RSS generation class instance

Source: Internet
Author: User
Tags foreach

PHP Implementation of the RSS generation class instance

This article mainly introduces the PHP implementation of the RSS generation class, example analysis of the RSS generated class principle, definition and use of skills, very practical value, the need for friends can refer to the

This article describes the RSS-generated class for PHP implementations. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 Class RSS {var $title var $link var $description var $language = "en-us"; var $pubDate; var $items; var $tags; functi On RSS () {$this->items = array (); $this->tags = Array ();} function AddItem ($item) {$this->items[] = $item;} f Unction 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. = "&L T;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 $x ml 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->PUBD ate = 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; The 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 </$keyn >"; $out. = "</item>n"; return $out; Function Enclosure ($url, $mimetype, $Length) {$this->attachment = $url; $this->mimetype = $mimetype; $this->length = $length;}}

Use the example below:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 $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.