PHP Perfect RSS-generation class

Source: Internet
Author: User
Tags foreach cdata comments generator

<?php

Class Rssgenerator_rss
{
var $rss _version = ' 2.0 ';
var $encoding = ';
var $stylesheet = ';

function CData ($STR)
{
Return ' <! [cdata['. $str. ']]> ';
}

function Createfeed ($channel)


{


$SELFURL = (!isset ($_server[' https ') | | | | $_server[' HTTPS ']!= ' on '? ' http://': ' HTTPS Tutorial://');


$selfUrl. = $_server[' Http_host ']. $_server[' php_self '];


$rss = ' &lt;?xml version= ' 1.0 ';


if (!empty ($this-&gt;encoding)) {


$rss. = ' encoding= '. $this-&gt;encoding. '"';


}


$rss. = '?&gt; '. " ";


if (!empty ($this-&gt;stylesheet)) {


$rss. = $this-&gt;stylesheet. " ";


}


$rss. = ' &lt;!--generated on '. Date (' R '). '--&gt; '. " ";


$rss. = ' &lt;rss version= '. $this-&gt;rss_version. ' xmlns:atom= ', ' &gt;http://www.w3.org/2005/atom ' &gt; '. " ";


$rss. = ' &lt;channel&gt; '. " ";


$rss. = ' &lt;atom:link href= '. ($channel-&gt;atomlinkhref $channel-&gt;atomlinkhref: $SELFURL). ' Rel= ' self ' type= ' application/rss+xml '/&gt; '. " ";


$rss. = ' &lt;title&gt; '. $channel-&gt;title. ' &lt;/title&gt; '. " ";


$rss. = ' &lt;link&gt; '. $channel-&gt;link. ' &lt;/link&gt; '. " ";


$rss. = ' &lt;description&gt; '. $channel-&gt;description. ' &lt;/description&gt; '. " ";


if (!empty ($channel-&gt;language)) {


$rss. = ' &lt;language&gt; '. $channel-&gt;language. ' &lt;/language&gt; '. " ";


}


if (!empty ($channel-&gt;copyright)) {


$rss. = ' &lt;copyright&gt; '. $channel-&gt;copyright. ' &lt;/copyright&gt; '. " ";


}


if (!empty ($channel-&gt;managingeditor)) {


$rss. = ' &lt;managingEditor&gt; '. $channel-&gt;managingeditor. ' &lt;/managingEditor&gt; '. " ";


}


if (!empty ($channel-&gt;webmaster)) {


$rss. = ' &lt;webMaster&gt; '. $channel-&gt;webmaster. ' &lt;/webMaster&gt; '. " ";


}


if (!empty ($channel-&gt;pubdate)) {


$rss. = ' &lt;pubDate&gt; '. $channel-&gt;pubdate. ' &lt;/pubDate&gt; '. " ";


}


if (!empty ($channel-&gt;lastbuilddate)) {


$rss. = ' &lt;lastBuildDate&gt; '. $channel-&gt;lastbuilddate. ' &lt;/lastBuildDate&gt; '. " ";


}


foreach ($channel-&gt;categories as $category) {


$rss. = ' &lt;category ';


if (!empty ($category [' domain '])) {


$rss. = ' domain= '. $category [' Domain ']. '"';


}


$rss. = ' &gt; '. $category [' name ']. ' &lt;/category&gt; '. " ";


}


if (!empty ($channel-&gt;generator)) {


$rss. = ' &lt;generator&gt; '. $channel-&gt;generator. ' &lt;/generator&gt; '. " ";


}


if (!empty ($channel-&gt;docs)) {


$rss. = ' &lt;docs&gt; '. $channel-&gt;docs. ' &lt;/docs&gt; '. " ";


}


if (!empty ($channel-&gt;ttl)) {


$rss. = ' &lt;ttl&gt; '. $channel-&gt;ttl. ' &lt;/ttl&gt; '. " ";


}


if (sizeof ($channel-&gt;skiphours)) {


$rss. = ' &lt;skipHours&gt; '. " ";


foreach ($channel-&gt;skiphours as $hour) {


$rss. = ' &lt;hour&gt; '. $hour. ' &lt;/hour&gt; '. " ";


}


$rss. = ' &lt;/skipHours&gt; '. " ";


}


if (sizeof ($channel-&gt;skipdays)) {


$rss. = ' &lt;skipDays&gt; '. " ";


foreach ($channel-&gt;skipdays as $day) {


$rss. = ' &lt;day&gt; '. $day. ' &lt;/day&gt; '. " ";


}


$rss. = ' &lt;/skipDays&gt; '. " ";


}


if (!empty ($channel-&gt;image)) {


$image = $channel-&gt;image;


$rss. = ' &lt;image&gt; '. " ";


$rss. = ' &lt;url&gt; '. $image-&gt;url. ' &lt;/url&gt; '. " ";


$rss. = ' &lt;title&gt; '. $image-&gt;title. ' &lt;/title&gt; '. " ";


$rss. = ' &lt;link&gt; '. $image-&gt;link. ' &lt;/link&gt; '. " ";


if ($image-&gt;width) {


$rss. = ' &lt;width&gt; '. $image-&gt;width. ' &lt;/width&gt; '. " ";


}


if ($image-&gt;height) {


$rss. = ' &lt;height&gt; '. $image-&gt;height. ' &lt;/height&gt; '. " ";


}


if (!empty ($image-&gt;description)) {


$rss. = ' &lt;description&gt; '. $image-&gt;description. ' &lt;/description&gt; '. " ";


}


$rss. = ' &lt;/image&gt; '. " ";


}


if (!empty ($channel-&gt;textinput)) {


$textInput = $channel-&gt;textinput;


$rss. = ' &lt;textInput&gt; '. " ";


$rss. = ' &lt;title&gt; '. $textInput-&gt;title. ' &lt;/title&gt; '. " ";


$rss. = ' &lt;description&gt; '. $textInput-&gt;description. ' &lt;/description&gt; '. " ";


$rss. = ' &lt;name&gt; '. $textInput-&gt;name. ' &lt;/name&gt; '. " ";


$rss. = ' &lt;link&gt; '. $textInput-&gt;link. ' &lt;/link&gt; '. " ";


$rss. = ' &lt;/textInput&gt; '. " ";


}


if (!empty ($channel-&gt;cloud_domain) | | |!empty ($channel-&gt;cloud_path) | |!empty ($channel-&gt;cloud_ Registerprocedure) | | !empty ($channel-&gt;cloud_protocol)) {


$rss. = ' &lt;cloud domain= '. $channel-&gt;cloud_domain. '" ';


$rss. = ' port= '. $channel-&gt;cloud_port. ' "Path=". $channel-&gt;cloud_path. '" ';


$rss. = ' registerprocedure= '. $channel-&gt;cloud_registerprocedure. '" ';


$rss. = ' protocol= '. $channel-&gt;cloud_protocol. ' "/&gt; '." ";


}


if (!empty ($channel-&gt;extraxml)) {


$rss. = $channel-&gt;extraxml. " ";


}


foreach ($channel-&gt;items as $item) {


$rss. = ' &lt;item&gt; '. " ";


if (!empty ($item-&gt;title)) {


$rss. = ' &lt;title&gt; '. $item-&gt;title. ' &lt;/title&gt; '. " ";


}


if (!empty ($item-&gt;description)) {


$rss. = ' &lt;description&gt; '. $item-&gt;description. ' &lt;/description&gt; '. " ";


}


if (!empty ($item-&gt;link)) {


$rss. = ' &lt;link&gt; '. $item-&gt;link. ' &lt;/link&gt; '. " ";


}


if (!empty ($item-&gt;pubdate)) {


$rss. = ' &lt;pubDate&gt; '. $item-&gt;pubdate. ' &lt;/pubDate&gt; '. " ";


}


if (!empty ($item-&gt;author)) {


$rss. = ' &lt;author&gt; '. $item-&gt;author. ' &lt;/author&gt; '. " ";


}


if (!empty ($item-&gt;comments)) {


$rss. = ' &lt;comments&gt; '. $item-&gt;comments. ' &lt;/comments&gt; '. " ";


}


if (!empty ($item-&gt;guid)) {


$rss. = ' &lt;guid ispermalink= ';


$rss. = ($item-&gt;guid_ispermalink? ' True ': ' false '. ' &gt; ';


$rss. = $item-&gt;guid. ' &lt;/guid&gt; '. " ";


}


if (!empty ($item-&gt;source)) {


$rss. = ' &lt;source url= '. $item-&gt;source_url. ' &gt; ';


$rss. = $item-&gt;source. ' &lt;/source&gt; '. " ";


}


if (!empty ($item-&gt;enclosure_url) | | |!empty ($item-&gt;enclosure_type)) {


$rss. = ' &lt;enclosure url= '. $item-&gt;enclosure_url. '" ';


$rss. = ' length= '. $item-&gt;enclosure_length. '" ';


$rss. = ' type= '. $item-&gt;enclosure_type. ' "/&gt; '." ";


}


foreach ($item-&gt;categories as $category) {


$rss. = ' &lt;category ';


if (!empty ($category [' domain '])) {


$rss. = ' domain= '. $category [' Domain ']. '"';


}


$rss. = ' &gt; '. $category [' name ']. ' &lt;/category&gt; '. " ";


}


$rss. = ' &lt;/item&gt; '. " ";


}


$rss. = ' &lt;/channel&gt; '. " ";


return $rss. = ' &lt;/rss&gt; ';


}

}

Class Rssgenerator_channel


{


var $atomLinkHref = ';


var $title = ';


var $link = ';


var $description = ';


var $language = ';


var $copyright = ';


var $managingEditor = ';


var $webMaster = ';


var $pubDate = ';


var $lastBuildDate = ';


var $categories = array ();


var $generator = ';


var $docs = ';


var $ttl = ';


var $image = ';


var $textInput = ';


var $skipHours = array ();


var $skipDays = array ();


var $cloud _domain = ';


var $cloud _port = ' 80 ';


var $cloud _path = ';


var $cloud _registerprocedure = ';


var $cloud _protocol = ';


var $items = array ();


var $extraXML = ';

}

Class Rssgenerator_image
{
var $url = ';
var $title = ';
var $link = ';
var $width = ' 88 ';
var $height = ' 31 ';
var $description = ';

}

Class Rssgenerator_textinput
{
var $title = ';
var $description = ';
var $name = ';
var $link = ';

}

Class Rssgenerator_item
{
var $title = ';
var $description = ';
var $link = ';
var $author = ';
var $pubDate = ';
var $comments = ';
var $guid = ';
var $guid _ispermalink = true;
var $source = ';
var $source _url = ';
var $enclosure _url = ';
var $enclosure _length = ' 0 ';
var $enclosure _type = ';
var $categories = array ();

}

?>

Instance

<?php

Require_once ' rss_generator.inc.php ';

$rss _channel = new Rssgenerator_channel ();
$rss _channel->atomlinkhref = ';
$rss _channel->title = ' my News ';
$rss _channel->link = ' http://111cn.net tutorial/news.php ';
$rss _channel->description = ' The latest news about Web-development. '
$rss _channel->language = ' en-US ';
$rss _channel->generator = ' PHP rss feed generator ';
$rss _channel->managingeditor = ' editor@mysite.com (Alex Jefferson) ';
$rss _channel->webmaster = ' webmaster@mysite.com (Vagharshak Tozalakyan) ';

$item = new Rssgenerator_item ();
$item->title = ' New website launched ';
$item->description = ' Today I finaly launch a new website. '
$item->link = ' http://111cn.net ';
$item->guid = ' http://111cn.net ';
$item->pubdate = ' Tue, 2006 00:00:01 GMT ';
$rss _channel->items[] = $item;

$item = new Rssgenerator_item ();
$item->title = ' Another website launched ';
$item->description = ' Just another website launched. '
$item->link = ' http://111cn.net ';
$item->guid = ' http://111cn.net ';
$item->pubdate = ' Wed, 2006 00:00:01 GMT ';
$rss _channel->items[] = $item;

$rss _feed = new Rssgenerator_rss ();
$rss _feed->encoding = ' UTF-8 ';
$rss _feed->version = ' 2.0 ';
Header (' Content-type:text/xml ');
echo $rss _feed->createfeed ($rss _channel);

?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.