Creating RSS XML feed Using PHP SimpleXML
In previous article I had created a RSS feed using PHP DOMDocument class, and today we are going to use PHP SimpleXML to create the same. PHP SimpleXML can be used to manipulate XML documents easily, it can read data from XML files/strings, and edit nodes and attributes. SimpleXML functions are part of PHP 5+, so everyone using PHP 5 and up requires no additional extensions to use its functions.
Previously I talked about fetching records from MySql database, here too we will use same database, and convert it into nice RSS feed for syndication. We first need to set document header content to XML, because browser needs to understand what we are throwing at it.
PHP
12
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
And then we use PHP SimpleXML to create XML elements for our RSS feed. Whole process is pretty simple, only bit confusing part is adding nodes, which I am sure you can manage. Then using using MySqli, we loop through each rows in the database table to create item nodes for the RSS. Here’s the final code you can play with.
PHP
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
<?php
$mysql_host = 'localhost'; //host
$mysql_username = 'root'; //username
$mysql_password = ''; //password
$mysql_database = 'test'; //db
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
$rss = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
$rss->addAttribute('version', '2.0');
$channel = $rss->addChild('channel'); //add channel node
$atom = $rss->addChild('atom:atom:link'); //add atom node
$atom->addAttribute('href', 'http://localhost'); //add atom node attribute
$atom->addAttribute('rel', 'self');
$atom->addAttribute('type', 'application/rss+xml');
$title = $rss->addChild('title','Sanwebe'); //title of the feed
$description = $rss->addChild('description','description line goes here'); //feed description
$link = $rss->addChild('link','http://www.sanwebe.com'); //feed site
$language = $rss->addChild('language','en-us'); //language
//Create RFC822 Date format to comply with RFC822
$date_f = date("D, d M Y H:i:s T", time());
$build_date = gmdate(DATE_RFC2822, strtotime($date_f));
$lastBuildDate = $rss->addChild('lastBuildDate',$date_f); //feed last build date
$generator = $rss->addChild('generator','PHP Simple XML'); //add generator node
//connect to MySQL - mysqli(HOST, USERNAME, PASSWORD, DATABASE);
$mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$results = $mysqli->query("SELECT id, title, content, published FROM site_contents");
if($results){ //we have records
while($row = $results->fetch_object()) //loop through each row
{
$item = $rss->addChild('item'); //add item node
$title = $item->addChild('title', $row->title); //add title node under item
$link = $item->addChild('link', 'http://www.your-site.com/link/goes/here/'); //add link node under item
$guid = $item->addChild('guid', 'http://www.your-site.com/link/goes/here/'. $row->id); //add guid node under item
$guid->addAttribute('isPermaLink', 'false'); //add guid node attribute
$description = $item->addChild('description', '<![CDATA['. htmlentities($row->content) . ']]>'); //add description
$date_rfc = gmdate(DATE_RFC2822, strtotime($row->published));
$item = $item->addChild('pubDate', $date_rfc); //add pubDate node
}
}
echo $rss->asXML(); //output XML
You can download file below and test it on your local server. You will also find a MySql file that you can import using phpMyAdmin and use it to test the snippet. All the best!
Download