Building a Generic RSS Class With PHP - What is RSS and How Does It Relate to XML? (Page 2 of 6 )
RSS stands for Rich Site Summary, and is an XML format that allows any Webmaster to share his news, articles, diary entries or whatever with other web sites. If you could consider MySQL as your permanent data store, then think of RSS as your business card for your web site.
RSS is in use by all of the big players on the Internet, including but not limited to SlashDot, CNN, ZDNet, Salon and Internet.com. RSS XML is generated as plain text, and each RSS XML file is called a channel, so SlashDot has a channel, ZDNet has a channel, etc.
The RSS specification started at version 0.91 in June 2000. It progressed to version 0.92 in December 2000, and now it's at version 2.0 although I prefer to stick with the 0.91 spec. There will be subsequent versions, however they will just be clarifications to the specification, not changes to the RSS format.
In terms of a relationship to XML, RSS is simply a strict format that you must generate your XML into. It defines a set of tags that your content must fit into. If you're familiar with XML, then heres how a typical RSS story looks:
<item> <title>My first article</title> <description>This is my first article, I hope you like it.</description> <link>http://www.devarticles.com/my_first_article.php</link> </item>
Each story in an RSS XML file is used to represent one article, one news headline, one diary entry, one recipe, etc. You can have up to 15 item blocks in your RSS XML file, however the less you have, the quicker the XML file is to parse for data.
As you can see in our example above, each story is bound by <item> and </item> tags and has a title, description and link. An RSS XML file actually contains more content than a couple of <item> tags, and we will now take a look at the syntax of a complete RSS XML file.
RSS XML Syntax RSS files allow you to syndicate your content into one file, which other webmasters can grab, parse and display on their web site - this is one of the best ways to drive instant traffic to your web site for little to no cost. In order for other webmasters to be able to use your RSS XML file, it needs to adhere to the correct syntax.
All RSS files are based on XML, and look similar - if not identical - to a normal XML file (accept for a few more element tags). To start with, our RSS file needs an XML version decleration tag:
Next up, we can specify a DTD (document type definition), which will be used to make sure our RSS file conforms to its syntax. Netscape invented the RSS spec, so we use their DTD:
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
We now need an RSS tag, which is the root element for our entire document. This tag simply looks like this:
As mentioned earlier, an RSS file is called a channel - a syndication if you like - for a web site. We use a channel tag to include metadata about our RSS feed. The channel tag looks like this:
It can include the following elements:
Title: The name of your RSS channel.
Description: The description of your RSS channel.
Link: A link to your web site's home page.
Language: A language encoding attribute, which signifies how your RSS file is encoded. We don't need to worry about this attribute, and can just use en-us, which means our RSS file is encoded in U.S. English.
Your channel tag can also include an image tag, which specifies the details and location to an image representing your site/channel. This image tag can include the following elements:
Title: A name for your image. Used if the person parsing your RSS XML file wants an ALT tag for your image.
URL: The URL to your image.
Link: Where your image should link (preferably your home page).
Here's how a channel tag might look for one web site:
The channel and image tags compose the "header" of our RSS XML file. The body of our RSS XML file is composed of several stories (item tags) that we discussed earlier, and might look something like this:
<item> <title>My first article</title> <description>This is my first article, I hope you like it.</description> <link>http://www.devarticles.com/my_first_article.php</link> </item> <item> <title>My second article</title> <description>This is my second article, I hope you like it.</description> <link>http://www.devarticles.com/my_second_article.php</link> </item>
Lastly, the root tag is closed, like this:
And that's all there is to creating a basic RSS XML file! You can include other attributes such as owner details, copyright details, etc, but I've left these out because they are optional - I also wanted to keep this explanation as simple as possible. To see the full RSS XML 0.91 spec, click here.
Before we move on, here's a complete RSS XML file (as displayed here):