Thursday, April 14, 2011

Generate RSS Feed with PHP

Hi,

If you are looking for code to generate RSS via PHP, then I guess you already know what RSS is. So I am coming straight to the source code for generating it. In my example below, I am generating a RSS feed for the listing details of vaiours movies, which would contain, the movie name, image, description and a link to the details page of the particular movie.

I am keeping a database table for storing the details of the various movies. The table schema is as follows:

CREATE TABLE IF NOT EXISTS `rss` (
  `id` int(10) NOT NULL auto_increment,
  `movie_name` varchar(100) collate latin1_general_ci default NULL,
  `category` varchar(50) collate latin1_general_ci default NULL,
  `pic` varchar(50) collate latin1_general_ci default NULL,
  `link` varchar(100) collate latin1_general_ci default NULL,
  `description` varchar(300) collate latin1_general_ci default NULL,
  PRIMARY KEY  (`id`)
)

As you see in the above schema, I have categorized the various movies. This will make it more dynamic. Now before going into the actual code and its explanation, lets see how the RSS feed looks like:



Code & Explanation:

First I have created a simple .htaccess file, for URL rewriting. My idea was, user will have the category_name.xml as the URL and the list will come according the category name. Here is the htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.xml$ example.php?q=$1 [QSA,L]

In the above, all the url's ending with XML will be sent to example.php and the xml file name as the query string i.e. "?q="

Now coming to the code. The file example.php does all the hard work with the following:

First the database connection is build and the SQL query as:

<?php
mysql_connect("localhost","root");
mysql_select_db("test");
$condition="";
if($_GET['q']!="all"){
$condition=$_GET['q'];
}

$q="select * from rss where category='".$condition."' or ''='".$condition."'";
$records=mysql_query($q);
?>


Here $_GET['q'] is the category name, in other words the xml file name. Now we are creating the RSS content by the following basic header tags:

<rss version="2.0">
<channel>
 <title><?php echo "PHP Drops :: ".ucwords($condition).' Movies'; ?></title>
 <link><?php echo "http://".$_SERVER['SERVER_NAME']; ?></link>
 <description><?php echo "PHP DROPS :: Generating RSS feed for Movies (SAMPLE)" ?></description>

 <language>en-us</language>
 <pubDate>Wednesday, 13 April 2011 04:00:00 GMT</pubDate>
 <lastBuildDate>Wednesday, 13 April 2011 04:00:00 GMT</lastBuildDate>
          <?php
 
           // code to generate the list of movies

          ?>
          </channel>
</rss>


Finally our code for the movie list:
<?php
while($movie=mysql_fetch_array($records)){
?>

   <item>
             <title><?php echo $movie['movie_name'];?></title>
            <link><?php echo $movie['link'];?></link>
            <description><?php echo htmlentities('<table width="700"><tr><td width="50"><img src="thumbnails/'.$movie['pic'].'" width="50"></td><td valign="top">'.$movie['description'].'</td></tr></table>');?></description>
            <pubDate>Wednesday, 13 April 2011 04:00:00 GMT</pubDate>
   </item>
<?php
}
?>

That's it. The tags are pretty much self explanatory, as to what means what. The interesting one is the description tag. You can actually put an HTML content inside it to decorate your RSS feed output. But what you need to keep in mind is, you need to put the PHP function htmlentities() if your tag content has any tag itself.


To run the code these are the URL's:

1. For viewing all movies under comedy category (comedy is the category mentioned in the database)
http://<YOUR_PATH>/comedy.xml

2. For viewing all movies under horror category (horror is the category mentioned in the database)
http://<YOUR_PATH>/horror.xml

3. For viewing all movies under action category (action is the category mentioned in the database)
http://<YOUR_PATH>/action.xml

4. For viewing all movies
http://<YOUR_PATH>/all.xml

Path I have used is http://localhost/php/rss/comedy.xml, where "rss/" contains the entire source code

Download entire source code with full database: rss.zip


No comments:

Post a Comment