This article mainly introduces the RSS generation class implemented by php. The example analyzes the principles, definitions, and usage skills of the RSS generation class, which is very useful. For more information, see
This article mainly introduces the RSS generation class implemented by php. The example analyzes the principles, definitions, and usage skills of the RSS generation class, which is very useful. For more information, see
This example describes the RSS generation class implemented by php. Share it with you for your reference. The details are 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;} 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. =" <$ key> $ val \ 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 = '<? Xml version = "1.0" encoding = "UTF-8"?> '. "\ 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. =" "; If (empty ($ this-> guid) $ this-> guid = $ this-> link; $ out. =" ". $ This-> guid ." \ N "; foreach ($ this-> tags as $ key => $ val) $ out. =" <$ key> $ val "; $ Out. =" \ N "; return $ out;} function enclosure ($ url, $ mimetype, $ length) {$ this-> attachment = $ url; $ this-> mimetype = $ mimetype; $ this-> length = $ length ;}}
Example:
$ 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 =" $html "; $ Feed-> addItem ($ item);} echo $ feed-> serve ();
I hope this article will help you with php programming.
,