Create an RSS feed with PHP

David Carr

2 min read - 29th May, 2013

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>

 

0 comments
Add a comment

Copyright © 2006 - 2024 DC Blog - All rights reserved.