Creating RSS XML feed with PHP DOMDocument

RSS makes it possible to syndicate latest website contents throughout the web seamlessly, whether you have a news website, blog or a normal website, you can break it down into discrete items and syndicated them via RSS. If you are using WordPress or any other popular CMS, you will find its own RSS system. But creating your custom RSS feed is also fairly easy using PHP. Let's get started! I will be using PHP DOMDocument class to construct the RSS document. In most circumstances, you want to fetch the records from database and turn the contents into RSS feed, we will be doing exactly that. Create a table in MySQL database using phpMyAdmin console, and insert few rows.
SQL
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
CREATE TABLE IF NOT EXISTS `site_contents` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(60) NOT NULL, `content` text NOT NULL, `published` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) );
Once the database is ready, we can write our XML generator code using PHP DOMDocument. First we start off by entering MySql database information, and then set the document header content type to XML, so that browsers understand it as XML document and render appropriately.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
<?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 ?>
The second line in the code below creates a new DOMDocument object, this object lets us build XML document using createElement, appendChild etc. The example creates XML document with node called "RSS".
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
<?php header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML $xml = new DOMDocument("1.0", "UTF-8"); // Create new DOM document. //create "RSS" element $rss = $xml->createElement("rss"); $rss_node = $xml->appendChild($rss); //add RSS element to XML node $rss_node->setAttribute("version","2.0"); //set RSS version echo $xml->saveXML(); //output: //<?xml version="1.0" encoding="UTF-8"?> //<rss version="2.0"/> ?>
So basically we build XML document using createElement(), setAttribute(), appendChild() and saveXML(). It sounds really easy, but it can get bit complicated as you start adding nodes. Here's the final code that generates RSS document for your website.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
<?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 $xml = new DOMDocument("1.0", "UTF-8"); // Create new DOM document. //create "RSS" element $rss = $xml->createElement("rss"); $rss_node = $xml->appendChild($rss); //add RSS element to XML node $rss_node->setAttribute("version","2.0"); //set RSS version //set attributes $rss_node->setAttribute("xmlns:dc","http://purl.org/dc/elements/1.1/"); //xmlns:dc (info http://j.mp/1mHIl8e ) $rss_node->setAttribute("xmlns:content","http://purl.org/rss/1.0/modules/content/"); //xmlns:content (info http://j.mp/1og3n2W) $rss_node->setAttribute("xmlns:atom","http://www.w3.org/2005/Atom");//xmlns:atom (http://j.mp/1tErCYX ) //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)); //create "channel" element under "RSS" element $channel = $xml->createElement("channel"); $channel_node = $rss_node->appendChild($channel); //a feed should contain an atom:link element (info http://j.mp/1nuzqeC) $channel_atom_link = $xml->createElement("atom:link"); $channel_atom_link->setAttribute("href","http://localhost"); //url of the feed $channel_atom_link->setAttribute("rel","self"); $channel_atom_link->setAttribute("type","application/rss+xml"); $channel_node->appendChild($channel_atom_link); //add general elements under "channel" node $channel_node->appendChild($xml->createElement("title", "Sanwebe")); //title $channel_node->appendChild($xml->createElement("description", "description line goes here")); //description $channel_node->appendChild($xml->createElement("link", "http://www.sanwebe.com")); //website link $channel_node->appendChild($xml->createElement("language", "en-us")); //language $channel_node->appendChild($xml->createElement("lastBuildDate", $build_date)); //last build date $channel_node->appendChild($xml->createElement("generator", "PHP DOMDocument")); //generator //Fetch records from the database //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); } //MySQL query, we pull records from site_contents table $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_node = $channel_node->appendChild($xml->createElement("item")); //create a new node called "item" $title_node = $item_node->appendChild($xml->createElement("title", $row->title)); //Add Title under "item" $link_node = $item_node->appendChild($xml->createElement("link", "http://www.your-site.com/link/goes/here/")); //add link node under "item" //Unique identifier for the item (GUID) $guid_link = $xml->createElement("guid", "http://www.your-site.com/link/goes/here/". $row->id); $guid_link->setAttribute("isPermaLink","false"); $guid_node = $item_node->appendChild($guid_link); //create "description" node under "item" $description_node = $item_node->appendChild($xml->createElement("description")); //fill description node with CDATA content $description_contents = $xml->createCDATASection(htmlentities($row->content)); $description_node->appendChild($description_contents); //Published date $date_rfc = gmdate(DATE_RFC2822, strtotime($row->published)); $pub_date = $xml->createElement("pubDate", $date_rfc); $pub_date_node = $item_node->appendChild($pub_date); } } echo $xml->saveXML(); ?>
I have created sample file, which can be downloaded from this page. I suggest that you test and play with the example file to understand how we use PHP DOMDocument class to create XML documents. If you want to create RSS using PHP SimpleXML functions, here is another example. Download
    New question is currently disabled!