Categories » PHP » PHP Generated RSS Feed
In this tutorial you will learn how to create a dynamic PHP based RSS feed. It is recommended that you have basic knowledge of php and MySQL before attempting this tutorial.
First we need to create a new php file. This can be done in any program such as Macromedia Dream Weaver or notepad, but just use whatever you feel comfortable with. Create a new file and save it , you can name it whatever you want, as long as it is a php file, but in this tutorial we will call it feed.php.
Now we connect to your database and declare this file as a valid RSS feed so it will be read properly by the RSS reader. At this point your whole file should look like the following.
  PHP: | |
1 23 45 67 89 1011 1213 14 | <?php $dbuname = 'username';$dbpass = 'password'; $dbhost = 'hostname';$dbname = 'databasename'; $mainconnection = mysql_connect($dbhost, $dbuname,$dbpass); mysql_select_db($dbname, $mainconnection); // This declares this file as an RSS feed header('Content-type: text/xml');echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> |
Simply substitute your database details. The echoed statement declares this file as a valid RSS/XML file when the browser goes to parse it. Now that we have declared this file as a RSS feed and connected to the database we can begin to generate the actual feed. Now add the following to your script after the mysql connect commands above.
  PHP: | |
1 23 45 67 8 | echo " <rss><channel> <title>Name of your feed</title><description>Small description of the feed</description><link>Link to your website or the feed's website</link> "; |
In the code above, <channel> opens up your actual feed, <title> is the actual display name of your feed, <description> describes it, and <link> is your website or the feeds website. After that is added we can begin to create the items in your feed. To do this we will use a php loop to retrieve something from your database and loop it for each instance an item is in the your database. This is where the MySQL knowledge comes in handy. The table and column names in the mysql query below will vary based on your database structure.
  PHP: | |
1 23 45 67 89 1011 12 | $tutorialfetch = mysql_query('SELECT name,title,description FROM feed'); while($row = mysql_fetch_array($tutorialfetch)){ $tutorialtitle = $row['title']; $tutorialdescription = $row['description']; echo " <item> <link>link to item</link> <title>$tutorialtitle</title> <description>$tutorialdescription</description> </item> "; } |
In the code above, "Link to item" is the actual link to the story or object that this RSS entry is about. This can be generated many ways, for example on our website we use the tutorial's name and plug it into a php $_GET variable. $tutorialtitle defines the name of this title as displayed to the user. $tutorialdescription is the description of the content, normally an actual description or part of its contents. Those variables are looped with a simple WHILE loop, and then outputted to the page with an echo statement. Now that our feed items have been outputted we can close up the feed by adding the following to the bottom.
  PHP: | |
1 | echo "</channel></rss>"; |
This just simply ends our RSS file. Your completed file should now look like the following.
  PHP: | |
1 23 45 67 89 1011 1213 1415 1617 1819 2021 2223 2425 2627 2829 3031 3233 3435 3637 3839 40 | <?php $dbuname = 'username';$dbpass = 'password'; $dbhost = 'hostname';$dbname = 'databasename'; $mainconnection = mysql_connect($dbhost, $dbuname,$dbpass); mysql_select_db($dbname, $mainconnection); // This declares this file as an RSS feedheader('Content-type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>'; echo "<rss> <channel><title>Name of your feed</title> <description>Small description of thefeed</description> <link>Link to your website or the feed's website</link>"; $tutorialfetch = mysql_query('SELECT name,title,description FROM feed'); while($row = mysql_fetch_array($tutorialfetch)){ $tutorialtitle = $row['title']; $tutorialdescription = $row['description']; echo " <item> <link>link to item</link> <br /> <title>$tutorialtitle</title><br /> <description>$tutorialdescription</description> <br /> </item> ";} echo '</channel></rss>'; ?> |
We hope this tutorial was of some use to you and your website. If you need and support or help with this tutorial please feel free to email me.
PHP Generated RSS Feed
Category: PHP
Difficulty: 4/10
Views: 1189
Date Created: August 19, 2008
Date Modified: December 7, 2008
Category: PHP
Difficulty: 4/10
Views: 1189
Date Created: August 19, 2008
Date Modified: December 7, 2008

