PHP Perfect RSS-generation class

Source: Internet
Author: User
Tags cdata generator php class
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 ("title of the Log", "Address of the log", "Summary of the log", "date of publication of the Journal");
$RSS->display ()//Output RSS content


All the code is as follows:

The code is as follows Copy Code
<?php
// +----------------------------------------------------------------------
// | Yblog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.111cn.net/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 Channel Link
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $channel _link = ';
/**
+----------------------------------------------------------
* RSS Channel Description
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $channel _description = ';
/**
+----------------------------------------------------------
* 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, defaults to today
+----------------------------------------------------------
* @var String
* @access protected
+----------------------------------------------------------
*/
protected $pubDate = ';
protected $lastBuildDate = ';

protected $generator = ' yblog RSS generator ';

/**
+----------------------------------------------------------
* An array of RSS-feed information
+----------------------------------------------------------
* @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 the value of a string $value variable
+----------------------------------------------------------
*/
Public Function Config ($key, $value)
{
$this->{$key} = $value;
}

/**
+----------------------------------------------------------
* Add RSS entry
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* @param string $title The title of the log
* @param string $link log link
* @param string $description A summary of the log
* @param string $pubDate The publication date of the journal
+----------------------------------------------------------
*/
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 = "<?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
}
}
?>
Related Article

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.