RSS generation instance implemented by php

Source: Internet
Author: User
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.

,

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.