PHP Perfect RSS Generation Class _php Tutorial

Source: Internet
Author: User
Tags cdata
RSS subscription function, in many sites can have but there are many, the following code is their own writing, which used to a PHP class: RSS.class.php, feel very convenient, dare not exclusive, special take out to share with you.
The code is as follows Copy Code

Include_once ("class/rss.class.php");//Introduction of RSS PHP class
$RSS = new RSS ("Name", "Address", "description", "RSS channel icon");
$RSS->additem ("Log title", "Log Address", "Summary of Logs", "Log release date");
$RSS->display ();//Output RSS content


The full code is as follows:

The code is as follows Copy Code
// +----------------------------------------------------------------------
// | Yblog
// +----------------------------------------------------------------------
// | Copyright (c) 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 Channel Link
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $channel _link = "';
/**
+----------------------------------------------------------
* RSS Channel Description
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $channel _description = ";
/**
+----------------------------------------------------------
* The URL of the small icon used by the RSS channel
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $channel _imgurl = ";
/**
+----------------------------------------------------------
* The language used by the RSS channel
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $language = ' ZH_CN ';
/**
+----------------------------------------------------------
* RSS Document creation date, default is today
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $pubDate = ";
protected $lastBuildDate = ";

protected $generator = ' yblog RSS generator ';

/**
+----------------------------------------------------------
* An array of RSS messages
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
Protected $items = Array ();

/**
+----------------------------------------------------------
* Constructor function
+----------------------------------------------------------
* @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 The value of the variable
+----------------------------------------------------------
*/
Public Function Config ($key, $value)
{
$this->{$key} = $value;
}

/**
+----------------------------------------------------------
* Add RSS entries
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* @param string $title The title of the log
* @param string $link A link to the log
* @param string $description A summary of the log
* @param string $pubDate The publication date of the log
+----------------------------------------------------------
*/
function AddItem ($title, $link, $description, $pubDate)
{
$this->items[] = Array (' title ' = = $title, ' link ' = ' $link, ' description ' = ' $description, ' pubDate ' + $pub Date);
}

/**
+----------------------------------------------------------
* Output RSS XML As String
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* @return String
+----------------------------------------------------------
*/
Public Function Fetch ()
{
$rss = " RN ";
$rss = " RN ";
$rss. = " RN ";
$rss. = " <title><! [Cdata[{$this->channel_title}]]></title>RN ";
$rss. = " {$this-> channel_description}]] > </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. = " <title><! [Cdata[{$this->channel_title}]]></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
}
}
?>

http://www.bkjia.com/PHPjc/444698.html www.bkjia.com true http://www.bkjia.com/PHPjc/444698.html techarticle RSS subscription function, in many sites can have but there are many, the following code is written by themselves, which used to a PHP class: RSS.class.php, feel very convenient, dare not exclusive, special take ...

  • 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.