Re: RSS feeder in PHP5?

From: Date: Mon, 12 Jan 2009 11:25:57 +0000
Subject: Re: RSS feeder in PHP5?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-286077@lists.php.net to get a copy of this message
Craig Whitmore wrote:
On Mon, 2009-01-12 at 11:02 +0000, Richard Heyes wrote:
Is there something in PHP5 which can generate the RSS feed?
You don't need an extension to help you generate an XML feed. You dimply output XML data instead of HTML and send an appropriate content type header, eg: header('Content-Type: text/xml');
You actually mean application/xml not text/xml
actually.. header("Content-type: application/rss+xml; charset=iso-8859-1") or header("Content-type: application/rss+xml; charset=utf-8")
And its alot better to use DOMDocument in PHP5 for XML Creation rather than hardcode everything.
agreed in principle; and I can't belive i'm saying this.. but with the case of a single rss feed in a single format then a tiny inline script which echo's out is far far lighter on the server and will do the trick; it's one of those set-up and leave scripts so DOMDocument may be overkill in this situation. here's an example / test code: <?php $rss_item_template = ' <item> <title>RSS_TITLE</title> <link>RSS_LINK</link> <pubDate>RSS_DATE</pubDate> <dc:creator>mediatakeout</dc:creator> <category><![CDATA[news]]></category> <guid isPermaLink="false">RSS_LINK</guid> <description><![CDATA[RSS_DESCRIPTION]]></description> <content:encoded><![CDATA[RSS_DESCRIPTION]]></content:encoded> </item> '; $rss_items = ''; $align = 'right'; if( $raw_items = $db->select("SELECT * FROM news LIMIT 0,20") ) { foreach($raw_items as $index => $item ) { $title = stripJunkSimple(stripslashes(htmlentities($item['title']))); $link = "http://domain.com/". $item['id']; //edit $date = date( 'r', $item['posting_date'] ); //edit $plain = str_replace("\n",'<br />',stripslashes($item['body'])); $in = array('RSS_TITLE','RSS_LINK','RSS_DATE','RSS_DESCRIPTION'); $out = array( $title, $link, $date, $plain ); $rss_simple_item = str_replace( $in , $out , $rss_item_template ); $rss_items .= $rss_simple_item; } } $rss_template = '<?xml version="1.0" encoding="iso-8859-1"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"
<channel>
<title>Domain.com RSS Feed</title> <atom:link href="http://' .'domain.com'. '/rss/" rel="self" type="application/rss+xml" /> <link>http://' .'domain.com'. '/</link> <description>Top Stories on Domain.com</description> <pubDate>' . date('r') . '</pubDate> <generator>http://' . 'domain.com' . '/</generator> <language>en</language> ' . $rss_items . ' </channel> </rss>'; header("Content-type: application/rss+xml; charset=iso-8859-1"); echo $rss_template; ?>

« previous php.general (#286077) next »