Home arrow MySQL arrow Page 2 - Building a Generic RSS Class With PHP
MYSQL

Building a Generic RSS Class With PHP


Ever wondered how to build an RSS XML feed from your site using PHP? In this article Mitchell explains what RSS is, and steps you through building a generic RSS generating class with PHP and MySQL.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
November 05, 2002
TABLE OF CONTENTS:
  1. · Building a Generic RSS Class With PHP
  2. · What is RSS and How Does It Relate to XML?
  3. · Building Our PHP Class
  4. · The GetRSS Function
  5. · Testing Our Class
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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 here’s 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:

<?xml version="1.0"?>

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:

<rss version="0.91">

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:

<channel>

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:

<channel>
<title>My Sample Channel</title>
<link>http://www.mysite.com</link>
<description>My sample RSS channel</description>
<language>en-us</language>
</channel>


Following the channel tag, you can optionally include an image tag:

<image>
<title>My Sample Image</title>
<url>http://www.mysite.com/mylogo.gif</url>
<link>http://www.mysite.com</link>
</image>


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:

</rss>

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):

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/">

<channel>
<title>UT Web Central Spotlights</title>
<link>http://www.utexas.edu/spotlight/</link>
<description>Spotlighted items on Web central and other pages</description>
</channel>

<image>
<title>UT Austin</title>
<url>http://www.utexas.edu/graphics/star_with_ut.gif</url>
<link>http://www.utexas.edu/</link>
</image>

<item>
<title>New UT Austin Travel Program</title>
<link>http://www.utexas.edu/business/travel/travelchanges.html</link>
</item>

<item>
<title>Annual Enrollment 2000</title>
<link>http://www.utexas.edu/admin/ohr/irg/enroll/</link>
</item>

</rdf:RDF>


Note: To prove that this is valid RSS XML, you can try validating it here.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials