RSS – Creating a Feed (Part 3/3)

In the final article of this series we’re going to write a script in PHP that will dynamically create an RSS feed from entries in a MySQL database.  If you decide to use this script in your own custom application, you may need to change parts of the script, such as the query.  You can check out the script here.

Let’s take a look at the script to see how it works.

The first part will define some parameters to establish a database connection.  This includes information such as the host, username, password, and the database.

This next part is important.

header(‘Content-type: text/xml’);
print “<?xml version=\”1.0\”?>\n”;

This sends the appropriate header information to the client telling it that this is in fact an XML file. Without this line, your feed will show up as plain text and will be read as such.

After the header information, we begin the actual RSS feed. This should look familiar as it was covered in the previous article. Simply fill this information out as it relates to your site or have it retrieve the information from another source, such as your database.

<?php
 // Connection to db
 $mysqldb = new mysqli(HOST, USER, PASS, DB);
 // Query to select posts

 $query = “SELECT * FROM posts”;
 $result = $mysqldb->query($query);
 if($result->num_rows) {
  while($row = $result->fetch_array(MYSQLI_ASSOC)) {
?>

   <item>
    <title><?php echo $row['post_title']; ?></title>
    <link><?php echo $row['post_link']; ?></link>
    <author><?php echo $row['post_author']; ?></author>
    <description><?php echo $row['post_content']; ?></description>
   </item>
<?php 
  }
 }
?>

This is the part of the code that fetches the posts from the database and turns them into the RSS feed. Simply retrieve all of your posts from the databse and use a while loop to print them all out as items of the feed. Once the loop is done, close the channel and RSS tags and you’re done!  You now have an RSS feed that will update itself when you update your MySQL database.

RSS – Inside (Part 2)

In the previous article I demonstrated a practical application of RSS with Google Reader.

Now that we have a working example of RSS in action, let’s dig a bit deeper and see what’s inside an RSS feed. For this article, we’re going to use RSS 2.0 as our primary example.

An RSS feed is made up of two main parts: information about our feed and the items in our feed.

Information about our feed describe what the feed is all about.  It includes elements such as the blog title, link to our blog and description of our blog. All of this information is mandatory.

The other part (items) can be thought of as each individual blog post. Each item has elements that describe the post. These elements include the title of the post, the link to the full post, the full content or snippet of the post, the author, and date published. All of these elements except the author and publication date is mandatory.

Let’s take a look at a sample RSS file

<?xml version="1.0"?>
<rss version="2.0">
        <channel>
        <title>nickpeters.net</title>
        <link>http://www.nickpeters.net</link>
        <description>Blog of Nick Peters</description>
        <item>
                <title>Blog Post 1</title>
                <link>http://www.nickpeters.net/blog-post-1</link>
                <author>Nick Peters</author>
                <description>This is a Blog Post!</description>
		<pubDate>Mon, 26 Mar 2007 09:39:21 GMT</pubDate>

        </item>
	<item>
                <title>Blog Post 2</title>
                <link>http://www.nickpeters.net/blog-post-2</link>
                <author>Nick Peters</author>
                <description>This is a another Blog Post!</description>
		<pubDate>Tue, 27 Mar 2007 011:52:38 GMT</pubDate>

        </item>
        </channel>
</rss>

Let’s first take note that this information is very minimal, but it should be enough to get key ideas across. A full list of elements can be found here.

From looking at the information above we can see that the name our blog is “nickpeters.net,” the URL of the blog is http://www.nickpeters.net and the description is “Blog of Nick Peters.”

This blog has two posts: “Blog Post 1” and “Blog Post 2.” Each post has a link, author, description and publication date.

RSS feeds are pretty self explanatory; it’s an XML file that describes the contents of a blog for easy manipulation in clients such as RSS readers.

Next we will see how easy it is to create an RSS feed using PHP and MySQL.

RSS – Introduction (Part 1)

Adam Darowski recently asked to pick my brain about writing a PHP script to create an RSS feed from a database. As one of my favorite “Web 2.0” technologies, I figured I would write a 3-part introductory series on RSS and PHP.

In this series I want to cover a couple topics:

  • What is RSS?

  • Digging deeper into RSS
  • Creating an RSS feed with PHP

Before we begin, let me pose a question: Are there sites you check frequently? Perhaps you check a friend’s blog on a daily basis or refresh a news site to get all of the latest headlines. This in itself can be time consuming, especially if you check multiple sites on a daily basis. RSS’s most popular application solves this problem; it will notify you when a site has been updated. Instead of checking your friend’s blog, you can subscribe and be notified of updates.

In order to use RSS for this purpose, we will need two things:

  • RSS Reader

  • RSS Feed

I prefer to use a web-based RSS reader such as Google Reader. (See this post to find out why)

Add RSS Feed

To add an RSS feed to Google Reader, click on “Add Subscription” and type in the URL of the site (nickpeters.net for example).

When we click “Add” we will now be subscribed and notified when nickpeters.net has been updated; It’s that simple.

Firefox2.0 has built in support for adding an RSS feed to the reader of your choice. See that RSS Icon in the corner of the address bar? What happens if we click on it? We’ll be redirected to another page that allows you to subscribe to the site using an RSS Reader of our choice.

Now that you have two ways to subscribe to a site, try this out for blogs or news sites you visit on a regular basis.

Next, we will take a look inside a basic RSS feed.

A new way to sort RSS feeds

I doubt this is new, but I just thought of it just now, so it’s new to me! I’m taking advantage of the Google Reader feature to group my RSS/Atom feeds by tagging them. This concept in itself isn’t new, but what I thought of was to take the feeds of certain tags from other sites and group them in Google Reader by their respective tag. For example, say I want to see what’s new in the world of Microformats. I can subscribe to the microformat ma.gnolia group, subscribe to the microformat search feed in technorati, and subscribe to bookmarks tagged with microformats in del.icio.us. All of these can be tagged and put under the microformat group in Google Reader and named by their respective web site for easy viewing.

RSS and Google Personalized Pages

One of the emerging technologies in this Web 2.0 fad is called “RSS” or Really Simple Syndication. This XML-based technology is used for a variety of purposes such as news feeds, podcasts, and sending out regularly updated information. As a matter of fact, this blog has an RSS feed; you can identify sites easily with Firefox because they have the icon, shown on the left, in the right hand side in the address bar.

So great, RSS sounds neat, but how is it used? RSS can be used with 3rd party applications, but instead I’m going to talk about Google Personalized Pages. I’ve noticed one of the most commonly used homepages for people is Google. How about we get some more use out of that Google homepage?

Do you have sites you visit on a daily or regular basis? Perhaps you have a news site that you check every morning at work? Chances are those sites have RSS feeds. For me, I check out sites such as Digg and Slashdot. I check these sites frequently because they are updated on a regular basis. Instead of going to the site, I can add the RSS feed to my Google personalized page. This feed will give me the title and URL of the of the news articles that have been recently added to the website.

So how do we start our Google personalized page? We first go to the Google homepage and find the link at the top right that says “Personalized Home.” Already we see a lot of new content including a calendar, weather, date & time, etc. To add our RSS feed we go to the “Add Content »” link at the top left of the page. Once there we can do one of two things: search for the RSS feed or add it manually. The easy way is to obviously search for it. For our example, we will add the Digg RSS feed. Simply type “digg” in the search field at the top, hit the search button, and click on “Add it Now” button next to the search result. To see the RSS feed, click on the “« Back to homepage” link. There you have it; we have the RSS feed that is dynamically updated when new content is added to the website.

So what if you don’t find the RSS feed in the search or you have the URL of an RSS feed you would like to add? Go back to the “Add Content” link. Next, go to the search form and click on the “Add by URL” link. In the new form that appears we will enter the URL of the RSS feed for the site. Continuing our example, we will add the Digg RSS feed: http://digg.com/rss/index.xml. After we add it, go back to your homepage and you will see the RSS feed has been added to the homepage.

Now that you know how to find and add RSS feeds to your Google Personalized Page, go ahead and rearrange the various feeds to your liking. Google makes it easy to do this by using drag and drop methods.

There is one last note I would like to add. Google personalized pages can be used anywhere you sign on. For example, I use this as my homepage at both work and home. If I make a change at work, it shows up when I come home and vice-versa. This happens because it uses your Gmail account to save your data. If you do not have a Gmail account, you may create a Google personalized page account to use for this purpose.