Php perfect rss generation class. 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 not exclusive. the RSS subscription function is 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: |
|
|
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: |
|
// + ----------------------------------------------------------------------
// | YBlog
// + ----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.hzhuti.com/nokia/n97/ All rights reserved.
// + ----------------------------------------------------------------------
// + ----------------------------------------------------------------------
// | Author: yhustc
// + ----------------------------------------------------------------------
// $ Id $
/**
+ ------------------------------------------------------------------------------
* RSS generation class
+ ------------------------------------------------------------------------------
* @ Author yhustc
* @ 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 =" Rn ";
$ Rss =" Rn ";
$ Rss. =" Rn ";
$ Rss. =" <! [CDATA [{$ this-> channel_title}]>Rn ";
$ Rss. =" {$this->channel_description} Rn ";
$ Rss. =" {$ This-> channel_link}Rn ";
$ Rss. =" {$ This-> language} Rn ";
If (! Empty ($ this-> pubDate ))
$ Rss. =" {$ This-> pubDate} Rn ";
If (! Empty ($ this-> lastBuildDate ))
$ Rss. =" {$ This-> lastBuildDate} Rn ";
If (! Empty ($ this-> generator ))
$ Rss. =" {$ This-> generator} Rn ";
$ Rss. =" 5 Rn ";
If (! Empty ($ this-> channel_imgurl )){
$ Rss. =" Rn ";
$ Rss. =" <! [CDATA [{$ this-> channel_title}]>Rn ";
$ Rss. =" {$ This-> channel_link}Rn ";
$ Rss. =" {$ This-> channel_imgurl} Rn ";
$ Rss. ="Rn ";
}
For ($ I = 0; $ I <count ($ this-> items); $ I ++ ){
$ Rss. =" Rn ";
$ Rss. =" <! [CDATA [{$ this-> items [$ I] ['title']}]>Rn ";
$ Rss. =" {$ This-> items [$ I] ['link']}Rn ";
$ Rss. =" {$this->items[$i]['description']} Rn ";
$ Rss. =" {$ This-> items [$ I] ['pubdate']} Rn ";
$ Rss. =" Rn ";
}
$ Rss. =" Rn ";
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;
}
}
?> |
...