Saturday, 9 February 2013

Easy Dynamic RSS Feed in PHP/MySQL

RSS feeds are practically a requirement now days for websites. If you have any sort of news, tutorials, articles that are updated regularly, an RSS feed is a must have. We are going to show you how to create a dynamic RSS feed to updates automatically when you have added something to your website.

Database Requirements

The feed requires the following information for each post that will be added:
  1. Title
  2. Content or summary
  3. Web address to the article
  4. The date it was published
Provided that you have the required information in your database already, we can move on.

1. Add the RSS 'Header' Info

The first step is to tell the browser that this file is an XML file. If you didn't already know, RSS is an XML file that uses a special defined syntax. To let the browser know this is an XML file, we add the following line:
header('Content-Type: text/xml');  
Now that we have told the browser that this is an XML file, we can start spitting out XML syntax for it to read. RSS has some 'Header' like fields to help define important variables about the entire feed:

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My Website Name</title>
<description>A description of the feed</description>
<link>The URL to the website</link>'; 

Let's run through the lines added in the code above. The first line that starts with '<?xml...' is simply a line that tells the browser that we are starting the XML code, what version to use and how it is encoded. Pretty much standard stuff. The next line (<rss...) tells the browser what version of rss we are using (in this case its 2). Then we start with <channel>. This is like <body> in HTML. It tells the browser 'Here comes the important information about this feed and its contents'. The <title> tags contain the title of the feed such as 'My RSS News Feed'. The <description> tags contain information about that describes the feed such as 'The latest news from my website'.  Finally, the <link> tags contain the full URL to the website. All this information is pretty much required and is often used on other websites to display the RSS feed.

2. Adding Articles to the Feed

Time for the dirty work. RSS feeds usually contain between 10-15 of the latest content on a website. So in this case, we are going to grab out the latest 15 articles from the database and display them in RSS XML:


 $get_articles = "SELECT id, title, summary,
DATE_FORMAT(added_date,'%a, %e %b %Y %T') as formatted_date
FROM articles ORDER BY added_date DESC LIMIT 15";

$articles = mysql_query($get_articles) or die(mysql_error());

while ($article = mysql_fetch_array($articles)){
     
    echo '
       <item>
          <title>'.$article[title].'</title>
          <description><![CDATA[
          '.$article[summary].'
          ]]></description>
          <link>http://www.mysite.com/article.php?id='.$article[article_id]</link>
          <pubDate>'.$row[formatted_date].' GMT</pubDate>
      </item>';
}

First, we write our query to get our articles. I have ordered by the date field descending to get the latest articles. I have also formatted the date using DATE_FORMAT() to convert the date field into a style that RSS likes. We then run the query using mysql_query() and die with an mysql_error() if there is an error.
We then do a while loop using mysql_fetch_array(). This will run through all the records retrieved and execute the code within the while loop for each record. In this loop we echo the structure of an RSS feed item. Firstly, we define the item with <item>. We then add the title of the article into the <title> tags. The <description> tags contains the summary or the full article. It is important that you wrap your description information with <![CDATA[ and close it with ]]>. This is to prevent any possible breakage if you have some HTML code within your description. The <link> tags provide a link to the article's web page and <pubDate> provides the date the article was added to the website.
All this information is required. 

3. Closing the RSS Feed

Now that we are done adding the articles to the feed, we need to close the RSS feed to let the browser know that there is no more content on the feed. We do this by adding the following lines after the loop:
 echo '</channel>
</rss>';

The lines above close the <channel> and <rss> tags to finish off the feed.
4. The Full Source
That is pretty much it! You can do alot of other things, just have a look at what other people use in their RSS feeds to get an idea. So, here is the full code:

<?php
header('Content-Type: text/xml');

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My Website Name</title>
<description>A description of the feed</description>
<link>The URL to the website</link>';
$get_articles = "SELECT id, title, summary,
DATE_FORMAT(added_date,'%a, %e %b %Y %T') as formatted_date
FROM articles ORDER BY added_date DESC LIMIT 15";

$articles = mysql_query($get_articles) or die(mysql_error());

while ($article = mysql_fetch_array($articles)){
     
    echo '
       <item>
          <title>'.$article[title].'</title>
          <description><![CDATA[
          '.$article[summary].'
          ]]></description>
          <link>http://www.mysite.com/article.php?id='.$article[article_id]</link>
          <pubDate>'.$row[formatted_date].' GMT</pubDate>
      </item>';
}
echo '</channel>
</rss>';
?>
Let us know how you get on with this tutorial by using the comments below.

2 comments: