Php perfect rss generation class

Source: Internet
Author: User

The RSS subscription function can be available on many websites, but there are also many. The following code is self-written, using a PHP class: RSS. class. php is very convenient and cannot be exclusive. I would like to share it with you.

The Code is as follows: Copy code

Include_once ("class/RSS. class. php"); // introduces the rss php class
$ RSS = new RSS ("name", "Address", "Description", "RSS channel icon ");
$ RSS-> AddItem ("log title", "log address", "log summary", "log release date ");
$ RSS-> Display (); // output RSS content


The Code is as follows:

The Code is as follows: Copy code
<? Php
// + ----------------------------------------------------------------------
// | YBlog
// + ----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.hzhuti.com/nokia/n97/ All rights reserved.
// + ----------------------------------------------------------------------
// + ----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// + ----------------------------------------------------------------------
// $ Id $
 
/**
+ ------------------------------------------------------------------------------
* RSS generation class
+ ------------------------------------------------------------------------------
* @ Author yhustc <yhustc@gmail.com>
* @ Version $ Id $
+ ------------------------------------------------------------------------------
*/
Class RSS
{
/**
+ ----------------------------------------------------------
* RSS channel name
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ channel_title = "';
/**
+ ----------------------------------------------------------
* RSS feed Link
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ channel_link = "';
/**
+ ----------------------------------------------------------
* RSS feed description
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ channel_description = '';
/**
+ ----------------------------------------------------------
* URL of the small icon used by the RSS Feed
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ channel_imgurl = '';
/**
+ ----------------------------------------------------------
* Language used by the RSS Feed
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ language = 'zh _ cn ';
/**
+ ----------------------------------------------------------
* Date on which an RSS document is created. The default value is today.
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ pubDate = '';
Protected $ lastBuildDate = '';
 
Protected $ generator = 'yblog RSS generator ';
 
/**
+ ----------------------------------------------------------
* Array of RSS single message
+ ----------------------------------------------------------
* @ Var string
* @ Access protected
+ ----------------------------------------------------------
*/
Protected $ items = array ();
 
/**
+ ----------------------------------------------------------
* Constructor
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param string $ title RSS channel name
* @ Param string $ link RSS Channel link
* @ Param string $ description RSS channel description
* @ Param string $ imgurl RSS channel icon
+ ----------------------------------------------------------
*/
Public function _ construct ($ title, $ link, $ description, $ imgurl = '')
{
$ This-> channel_title = $ title;
$ This-> channel_link = $ link;
$ This-> channel_description = $ description;
$ This-> channel_imgurl = $ imgurl;
$ This-> pubDate = Date ('Y-m-d H: I: s', time ());
$ This-> lastBuildDate = Date ('Y-m-d H: I: s', time ());
}
 
/**
+ ----------------------------------------------------------
* Set private variables
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param string $ key variable name
* @ Param string $ value variable value
+ ----------------------------------------------------------
*/
Public function Config ($ key, $ value)
{
$ This-> {$ key} = $ value;
}
 
/**
+ ----------------------------------------------------------
* Add an RSS entry
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param string $ title the log title
* @ Param string $ link log link
* @ Param string $ description log Summary
* @ Param string $ pubDate the log publication date
+ ----------------------------------------------------------
*/
Function AddItem ($ title, $ link, $ description, $ pubDate)
{
$ This-> items [] = array ('title' => $ title, 'link' => $ link, 'description' => $ description, 'pubdate' => $ pubDate );
}
 
/**
+ ----------------------------------------------------------
* The XML output of RSS is a string.
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Return string
+ ----------------------------------------------------------
*/
Public function Fetch ()
{
$ Rss = "<? Xml version = "1.0" encoding = "UTF-8"?> Rn ";
$ Rss = "<rss version =" 2.0 "> rn ";
$ Rss. = "<channel> rn ";
$ Rss. = "<title> <! [CDATA [{$ this-> channel_title}]> </title> rn ";
$ Rss. = "<description> <! [CDATA [{$ this-> channel_description}]> </description> rn ";
$ Rss. = "<link >{$ this-> channel_link} </link> rn ";
$ Rss. = "<language >{$ this-> language} </language> rn ";
 
If (! Empty ($ this-> pubDate ))
$ Rss. = "<pubDate >{$ this-> pubDate} </pubDate> rn ";
If (! Empty ($ this-> lastBuildDate ))
$ Rss. = "<lastBuildDate >{$ this-> lastBuildDate} </lastBuildDate> rn ";
If (! Empty ($ this-> generator ))
$ Rss. = "<generator >{$ this-> generator} </generator> rn ";
 
$ Rss. = "<ttl> 5 </ttl> rn ";
 
If (! Empty ($ this-> channel_imgurl )){
$ Rss. = "<image> rn ";
$ Rss. = "<title> <! [CDATA [{$ this-> channel_title}]> </title> rn ";
$ Rss. = "<link >{$ this-> channel_link} </link> rn ";
$ Rss. = "<url >{$ this-> channel_imgurl} </url> rn ";
$ Rss. = "</image> rn ";
}
 
For ($ I = 0; $ I <count ($ this-> items); $ I ++ ){
$ Rss. = "<item> rn ";
$ Rss. = "<title> <! [CDATA [{$ this-> items [$ I] ['title']}]> </title> rn ";
$ Rss. = "<link >{$ this-> items [$ I] ['link']} </link> rn ";
$ Rss. = "<description> <! [CDATA [{$ this-> items [$ I] ['description']}]> </description> rn ";
$ Rss. = "<pubDate >{$ this-> items [$ I] ['pubdate']} </pubDate> rn ";
$ Rss. = "</item> rn ";
}
 
$ Rss. = "</channel> rn </rss> ";
Return $ rss;
}
 
/**
+ ----------------------------------------------------------
* Output rss xml to the browser
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Return void
+ ----------------------------------------------------------
*/
Public function Display ()
{
Header ("Content-Type: text/xml; charset = UTF-8 ");
Echo $ this-> Fetch ();
Exit;
}
}
?>

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.