Here's the hypothetical situation. You're an enthusiastic weblogger. For your visitors' convenience, you want to offer them an RSS feed of your latest entries. Plus, you have a bad case of varicose veins.
Where do you start?
First, you will need to send the correct header information. Let your visitor's computer know XML code is oncoming.
Now, you're already ready to start sending your RSS code. If you know nothing about RSS, how it's formatted, etc., check this out: [http://blogs.law.harvard.edu/tech/rss
]. No need to feel ashamed, because, in fact, I know nothing about this subject matter either (don't lose faith in the credibility of this tutorial, though). Here is an example of what tags and data you should start sending first:
echo "<?xml version=\"1.0\"?>n";
echo "<rss version=\"2.0\">n";
echo " <channel>n";
echo " <title>dave's weblog</title>\n";
echo " <link>http://dave.nutang.com/</link>\n";
echo " <description>A blog written by mr. dave</description>\n";
echo " <language>en-us</language>\n";
echo " <generator>IceMelon RSS Feeder</generator>\n";
echo " <managingEditor>mr. dave</managingEditor>\n";
echo " <webMaster>firstname.lastname@example.org</webMaster>\n";
Next, come the <item> tags; AKA: your weblog entries. Each weblog entry you wish to post needs its own <item> stuff. Here's an example:
echo " <item>\n";
echo " <title>$weblogSubject</title>n";
echo " <link>http://www.nutang.com/bliss/weblog/dave/& amp;comment=$weblogID</link>\n";
echo " <pubDate>$weblogEntryDate</pubDate>\n";
echo " <description> <![CDATA[ $blogEntryItself ]]> </description>\n";
echo " </item>\n";
Finally, close off your XML/RSS tags:
echo " </channel>\n";
echo "</rss> ";
RSS readers are very nitpicky. If you've already implemented the above, test it out with this RSS validator [http://rss.scripting.com
]. If no errors are being reported, then you're either incredibly lucky, incredibly good, or.. your PHP code probably didn't even generate any RSS code.
If you do have errors, save yourself a lot of a debugging time by learning from my mistakes:
XML, like HTML, support escaped characters in the '&..;' format. Therefore, if you need to use an ampersand (&), escape it: & If you look at the sample code above, you'll see that's what I did for the weblog entry link.
XML doesn't support HTML tags. To properly format your data then, use htmlentities
. For example:
$blogEntryItself = htmlentities(strip_tags($blogEntryItself))
As an additional safety measure, notice that I already wrapped $blogEntryItself inside '<![CDATA[..]]>' (seen in the sample PHP-generated RSS code). The CDATA section is supposed to be ignored by the XML parser. However, I've found that it can still throw errors. Those errors were fixed by the htmlentities
Alright, that's a wrap.