PHP & MySQL, Tutorials

Create an RSS feed with PHP

First create a new php file called feed.php it needs to end in .php as php will be required in the file. As RSS feeds use XML the browser needs to be told to expect XML rather then php/html.

header('Content-type: application/xml');

Next connect to the database, change the username, pasword and database to match your own.

define('DB_SERVER', 'localhost');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database');
 
$conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Next add an RSS version tag and channel, this is needed to the feed items will go inside the channel. Also add the name for the feed, a short description and the website address.

echo "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
echo "<channel>\n";

echo "<title>Demo RSS Feed</title>\n";
echo "<description>RSS Description</description>\n";
echo "<link>http://www.mydomain.com</link>\n";

For the feed items loop through the database rows, in this case I'm using a news table.

$stmt = $conn->query('SELECT * FROM news ORDER BY newsDate DESC LIMIT 10');
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {

The feed items are stored in an item tag inside the item the following tags can be used:

  •     title
  •     description
  •     pubDate
  •     link
  •     guid
  •     atom

The following is the items in the loop, the date needs to be formatted to be a valid date formate for the RSS feed.

 echo "<item>\n";
     echo "<title>$row->newsTitle</title>\n";
     echo "<description>$row->newsDesc</description>\n";
     echo "<pubDate>".date('D, d M Y H:i:s',strtotime($row->newsDate))." GMT</pubDate>\n";
     echo "<link>http://www.mydomain.com/$row->newsSlug</link>\n";
     echo "<guid>http://www.mydomain.com/$row->newsSlug</guid>\n";
     echo "<atom:link href='http://www.mydomain.com/$row->newsSlug' rel='self' type='application/rss+xml'/>\n"
 echo "</item>\n";

After the loop close the channel and rss

echo "</channel>\n";
echo "</rss>\n";

Putting it all together:

<?php header('Content-type: application/xml'); 

define('DB_SERVER', 'localhost');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database');
 
$conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
echo "<channel>\n";

echo "<title>Demo RSS Feed</title>\n";
echo "<description>RSS Description</description>\n";
echo "<link>http://www.mydomain.com</link>\n";

 
$stmt = $conn->query('SELECT * FROM news ORDER BY newsDate DESC LIMIT 10');
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
    
     echo "<item>n";
         echo "<title>$row->newsTitle</title>\n";
         echo "<description>$row->newsDesc</description>\n";
         echo "<pubDate>".date('D, d M Y H:i:s',strtotime($row->newsDate))." GMT</pubDate>\n";
         echo "<link>http://www.mydomain.com/$row->newsSlug</link>\n";
         echo "<guid>http://www.mydomain.com/$row->newsSlug</guid>\n";
         echo "<atom:link href='http://www.mydomain.com/$row->newsSlug' rel='self' type='application/rss+xml'/>\n"
     echo "</item>\n";

}

echo "</channel>\n";
echo "</rss>\n";
?>

Extra Options

Images can be displayed in the description by using a CDATA tag followed by the image source, used with the description go inside the CDATA:

<description><![CDATA[<img src='pathtoimage' alt=''> $row->newsDesc]]></description>

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.