RSS newsfeed tutorial
From PRADO Wiki
To provide your users with RSS newsfeeds on your website just follow the steps described below. Good luck and happy coding! ;-)
--Andyknownasabu 19:08, 8 February 2008 (CST)
(1) Edit "protected/application.xml" and add a new entry in the services section. Two provide two different feeds on "products" and "services":
<service id="feed" class="System.Web.Services.TFeedService"> <feed id="products" class="Application.common.FeedProvider" /> <feed id="services" class="Application.common.FeedProvider" /> </service>
(2) Create a file "FeedProvider.php" in the directory "protected/common" as just defined in "application.xml":
<?php /** * FeedProvider.php * * Copyright 2008 by Andreas Bulling * All rights reserved. * * Terms of use and modification: * * You may modify the following source code as you receive it, in any medium, * provided that you conspicuously and appropriately retain the above * original copyright notice. * */ class FeedProvider implements IFeedContentProvider { private $_type; public function init($config) { $request = Prado::getApplication()->getRequest(); if ($request->contains('feed')) { $this->_type = TPropertyValue::ensureString($request['feed']); } } public function getContentType() { return 'application/rss+xml'; } public function getFeedContent() { $xmlString = ''; // load content switch( $this->_type ) { case 'products': $records = ... break; case 'services': $records = ... break; } // generate RSS feed string $feed = new TXmlDocument('1.0', 'UTF-8'); $feed->setTagName('rss'); $feed->setAttribute('version', '2.0'); $channel = new TXmlElement('channel'); $feed->getElements()->insertAt(0, $channel); $channelTitle = new TXmlElement('title'); $channelTitle->setValue('CHANNEL_TITLE'); $channel->getElements()->insertAt(0, $channelTitle); $channelLink = new TXmlElement('link'); $channelLink->setValue('http://foo.bar/index.php?feed=' . $this->_type); $channel->getElements()->insertAt(1, $channelLink); $channelDescription = new TXmlElement('description'); $channelDescription->setValue('CHANNEL_DESCRIPTION'); $channel->getElements()->insertAt(2, $channelDescription); $channelLanguage = new TXmlElement('language'); $channelLanguage->setValue( Prado::getApplication()->getGlobalization()->getCulture() ); $channel->getElements()->insertAt(3, $channelLanguage); $channelCopyright = new TXmlElement('copyright'); $channelCopyright->setValue('CHANNEL_COPYRIGHT'); $channel->getElements()->insertAt(4, $channelCopyright); $channelLastBuildDate = new TXmlElement('lastBuildDate'); $channelLastBuildDate->setValue(date('r')); $channel->getElements()->insertAt(5, $channelLastBuildDate); $channelGenerator = new TXmlElement('generator'); $channelGenerator->setValue('CHANNEL_GENERATOR'); $channel->getElements()->insertAt(6, $channelGenerator); $channelWebmaster = new TXmlElement('webMaster'); $channelWebmaster->setValue('CHANNEL_WEBMASTER'); $channel->getElements()->insertAt(7, $channelWebmaster); $channelImage = new TXmlElement('image'); $channel->getElements()->insertAt(8, $channelImage); $channelImageURL = new TXmlElement('url'); $channelImageURL->setValue('http://foo.bar/logo.gif'); $channelImage->getElements()->insertAt(0, $channelImageURL); $channelImageTitle = new TXmlElement('title'); $channelImageTitle->setValue('CHANNEL_TITLE'); $channelImage->getElements()->insertAt(1, $channelImageTitle); $channelImageLink = new TXmlElement('link'); $channelImageLink->setValue('CHANNEL_LINK'); $channelImage->getElements()->insertAt(2, $channelImageLink); foreach( $records as $record ) { $i = 0; $channelItems = new TXmlElement('item'); $itemTitle = new TXmlElement('title'); $title = utf8_encode($record->title); $itemTitle->setValue($title); $channelItems->getElements()->insertAt($i++, $itemTitle); $itemLink = new TXmlElement('link'); $itemLink->setValue('ITEM_LINK'); $channelItems->getElements()->insertAt($i++, $itemLink); $itemDesc = new TXmlElement('description'); $description = utf8_encode($record->description); $itemDesc->setValue($description); $channelItems->getElements()->insertAt($i++, $itemDesc); $itemAuthor = new TXmlElement('author'); $itemAuthor->setValue('ITEM_AUTHOR'); $channelItems->getElements()->insertAt($i++, $itemAuthor); $itemPubDate = new TXmlElement('pubDate'); $itemPubDate->setValue( date('r', $record->added_at) ); $channelItems->getElements()->insertAt($i++, $itemPubDate); $channel->getElements()->insertAt(9, $channelItems); } // return RSS news feed string return $feed->saveToString(); } } ?>
(3) Access your feeds by pointing your browser to one of the following URLs:
http://foo.bar/index.php?feed=products
http://foo.bar/index.php?feed=services
(4) Finally, you should also add the following lines inside the <head> section of your main template which makes browsers show a nice icon indicating that your website provides RSS newsfeeds:
<head> ... <link rel="alternate" type="application/rss+xml" title="Products" href="http://foo.bar/index.php?feed=products" /> <link rel="alternate" type="application/rss+xml" title="Services" href="http://foo.bar/index.php?feed=services" /> ... </head>

