<?xml version="1.0" encoding="UTF-8"?>
<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>Developer Tutorials' Webmaster Blog &#187; create rss</title>
	<atom:link href="http://www.developertutorials.com/blog/tag/create-rss/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developertutorials.com/blog</link>
	<description>Keeping webmasters up-to-date on technology.</description>
	<pubDate>Tue, 02 Sep 2008 14:59:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>RSS feeds in PHP: 3 simple steps to PHP RSS generation</title>
		<link>http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/</link>
		<comments>http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 10:45:41 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[create rss]]></category>

		<category><![CDATA[php content syndication]]></category>

		<category><![CDATA[php rss feed]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/</guid>
		<description><![CDATA[Syndication is well and truly here; just about every major content site has an RSS feed, the leading web browsers have inbuilt RSS readers and there are entire startups dedicated to helping you keep track of the sites that aggregate your feeds - let alone those that actually do the feed reading. Adding an RSS [...]]]></description>
			<content:encoded><![CDATA[<p>Syndication is well and truly here; just about every major content site has an RSS feed, the leading web browsers have inbuilt RSS readers and there are entire startups dedicated to helping you keep track of the sites that aggregate your feeds - let alone those that actually do the feed reading. Adding an RSS feed to your site is quick and painless, and can be done with pure PHP (and some kind of data source). After the jump, 3 simple steps to building an RSS feed in php.</p>
<p><span id="more-121"></span></p>
<p><strong>Step 1: Find your content.</strong><br />
An RSS feed should probably list all the content items in your main section. For example, if you run a blog, you can have an RSS feed of all your latest posts. <a href="http://www.developertutorials.com/blog/feed/">This blog does</a>. If you run a small download site, consider a feed of your latest releases. Whatever content you choose, make sure it&#8217;s interesting, and will be updated regularly - aim for between 3-6 times a week, if you have a choice. Then just write out your basic database logic and get the data into an array. I&#8217;ll use $items for this example.</p>
<p><strong>Step 2: Get your basic RSS structure.</strong><br />
RSS is an XML-based format, so all you really need is a set of XML tags as part of the RSS specification. RSS provides a number of optional tags, and being XML, it can be (and often is) extended with any other tag. For example, the <a href="http://dublincore.org/" target="_blank">Dublin Core metadata</a> is a common inclusion. You can borrow the basic structure from any feed, <a href="http://www.developertutorials.com/blog/feed/">including ours</a> (load it up and right click > view source), but here&#8217;s a sample you can use:</p>
<blockquote>
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;rss version="2.0"&gt;
&lt;channel&gt;
  &lt;title&gt;My Blog Name&lt;/title&gt;
  &lt;link&gt;http://example.com/blog&lt;/link&gt;
  &lt;description&gt;Keeping webmasters up-to-date on technology.&lt;/description&gt;
  &lt;pubDate&gt;Tue, 15 Apr 2008 18:00:00 +0000&lt;/pubDate&gt;
  &lt;item&gt;
    &lt;title&gt;Story Title&lt;/title&gt;
    &lt;pubDate&gt;Tue, 15 Apr 2008 18:00:00 +0000&lt;/pubDate&gt;
    &lt;link&gt;http://example.com/my/story&lt;/link&gt;
    &lt;description&gt;Today I saw a whale!&lt;/description&gt;
  &lt;/item&gt;
&lt;/channel&gt;
&lt;/rss&gt;
</pre>
</blockquote>
<p>The easiest way to build your RSS feed in PHP is to leave all of that XML as plain text and only loop over the sections as needed (the &lt;item&gt; tags). The one gotcha here, as you&#8217;ve probably noticed, is the opening &lt;?xml tag. This will probably clash with PHP, so instead echo that line out:</p>
<blockquote>
<pre>
&lt;?php echo "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; ?&gt;
</pre>
</blockquote>
<p>Then your structure is all in place. The <code>item</code> element is repeated for each story we have in our RSS feed; the date is RFC 2822 formatted (just use <code>date("r")</code>). We&#8217;ll use a simple <code>foreach</code> loop to put the content in:</p>
<blockquote>
<pre>
&lt;?php foreach ($items as $item) { ?&gt;
  &lt;item&gt;
    &lt;title&gt;&lt;?=$item['title']?&gt;&lt;/title&gt;
    &lt;pubDate&gt;&lt;?=date("r",$item['time'])?&gt;&lt;/pubDate&gt;
    &lt;link&gt;&lt;?=$item['url']?&gt;&lt;/link&gt;
    &lt;description&gt;&lt;?=$item['description']?&gt;&lt;/description&gt;
  &lt;/item&gt;
&lt;?php } ?&gt;
</pre>
</blockquote>
<p><strong>3. Serve it up!</strong><br />
To be recognised as an RSS feed, you&#8217;ll need to serve it as the right content type. Technically you should use application/rss+xml, but most browsers won&#8217;t recognise this. I find application/xhtml+xml works well, as does text/xml. The choice is yours, although IE7 seems to like text/xml better.</p>
<blockquote>
<pre>
&lt;?php header("Content-type: text/xml"); ?&gt;
</pre>
</blockquote>
<p><strong>And we&#8217;re done!</strong><br/><br />
Here&#8217;s a sample of the final product:</p>
<blockquote>
<pre>
&lt;?php header("Content-type: text/xml"); ?&gt;
&lt;?php echo "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; ?&gt;
&lt;rss version="2.0"&gt;
&lt;channel&gt;
  &lt;title&gt;My Blog Name&lt;/title&gt;
  &lt;link&gt;http://example.com/blog&lt;/link&gt;
  &lt;description&gt;Keeping webmasters up-to-date on technology.&lt;/description&gt;
  &lt;pubDate&gt;Tue, 15 Apr 2008 18:00:00 +0000&lt;/pubDate&gt;
&lt;?php foreach ($items as $item) { ?&gt;
  &lt;item&gt;
    &lt;title&gt;&lt;?=$item['title']?&gt;&lt;/title&gt;
    &lt;pubDate&gt;&lt;?=date("r",$item['time'])?&gt;&lt;/pubDate&gt;
    &lt;link&gt;&lt;?=$item['url']?&gt;&lt;/link&gt;
    &lt;description&gt;&lt;?=$item['description']?&gt;&lt;/description&gt;
  &lt;/item&gt;
&lt;?php } ?&gt;
&lt;/channel&gt;
&lt;/rss&gt;
</pre>
</blockquote>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark" style="float:left;">
<div class="d121" style="overflow:hidden">
<div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/&amp;title=RSS+feeds+in+PHP%3A+3+simple+steps+to+PHP+RSS+generation" title="Add to&nbsp;Del.icio.us">Del.icio.us</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/&amp;title=RSS+feeds+in+PHP%3A+3+simple+steps+to+PHP+RSS+generation" title="Add to&nbsp;Digg This">Digg This</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/&amp;title=RSS+feeds+in+PHP%3A+3+simple+steps+to+PHP+RSS+generation" title="Add to&nbsp;Stumble">Stumble</a></div></div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/rss-feeds-in-php-3-simple-steps-to-php-rss-generation-121/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
