<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLovin &#187; php</title>
	<atom:link href="http://www.maclovin.de/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maclovin.de</link>
	<description>An Apple a day keeps the Windows away</description>
	<lastBuildDate>Thu, 25 Feb 2010 20:57:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create RSS feeds with PHP</title>
		<link>http://www.maclovin.de/2009/03/create-rss-feeds-with-php/</link>
		<comments>http://www.maclovin.de/2009/03/create-rss-feeds-with-php/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 21:24:05 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.maclovin.de/?p=138</guid>
		<description><![CDATA[RSS is a format for publishing frequently changing content like news, a list of blog posts or directories. Using RSS readers like Google Reader or Apple Mail, you can read all your daily information at one place, offering a much better overview and faster processing than visiting every single website.
An RSS feed from the technical [...]]]></description>
			<content:encoded><![CDATA[<p>RSS is a format for publishing frequently changing content like news, a list of blog posts or directories. Using RSS readers like Google Reader or Apple Mail, you can read all your daily information at one place, offering a much better overview and faster processing than visiting every single website.</p>
<p>An RSS feed from the technical point of view is nothing more than a specially formatted XML document. It contains a general feed description as well as a headline, a link to the source and the content or an excerpt for every item. In this arcticle, I will show you how to create your own feed using PHP.</p>
<p><span id="more-138"></span></p>
<p>First of all, we set up the <strong>header information</strong> to specify that it is an XML document.</p>
<pre class="brush: php">&lt;?
header("Content-Type: text/xml; charset=utf-8");
print "&lt;?xml version=\"1.0\" ?&gt;\n";
?&gt;</pre>
<p>Then we add <strong>general feed information</strong> like the feed&#8217;s name, its description and the source URL.</p>
<pre class="brush: xml;first-line: 5">&lt;rss version="2.0"&gt;
&lt;channel&gt;

&lt;title&gt;Feed's title&lt;/title&gt;
&lt;description&gt;My site's cool feed&lt;/description&gt;
&lt;link&gt;http://www.my-site.com&lt;/link&gt;</pre>
<p>Now the real work begins. We need to get the necessary information for every item to be published. This could mean simply selecting information from a database, but also parsing XML or HTML files and gathering the information we need. It all depends on the datasource. When looping over every single item of our information, I assume we fill the variables <em>$item</em>, <em>$content</em> and <em>$link</em>, containing what the name suggests. We print the following block for every step of the loop.</p>
<pre class="brush: php;first-line: 35">&lt;item&gt;
  &lt;title&gt;&lt;?= $title ?&gt;&lt;/title&gt;
  &lt;description&gt;&lt;?= $content ?&gt;&lt;/description&gt;
  &lt;link&gt;&lt;?= $link ?&gt;&lt;/link&gt;
&lt;/item&gt;</pre>
<p>Finally, we close the opened tags.</p>
<pre class="brush: xml;first-line: 46">&lt;/channel&gt;
&lt;/rss&gt;</pre>
<p>That&#8217;s pretty much all you need to set up your own feed. But there are some pitfalls I discovered when creating my first feed. Because we&#8217;re working with XML documents, we have to convert special characters like &lt; and &gt; to &amp;lt; and &amp;gt;. This can be archieved by using PHP&#8217;s  <strong>htmlspecialchars</strong> funtion. However, I had to use it <em>twice</em> for a proper display in Google Reader. I would have expected Google to escape the content properly, but looks like they just pass the output to the frontend.</p>
<p>Now I wish you a lot of fun creating your own feed. If you need some information on how to parse HTML files with PHP and create  an RSS feed from almost any website, just leave a comment and I will provide some examples.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.maclovin.de/2009/03/create-rss-feeds-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
