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
  • 1
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
  • 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
<?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
  • Thanks for this great tutorial. Just one thing. Shouldn't be all the major element info about RSS hanged on channel instead of the rss element? If I do this like you're presenting, there would be and nothing else. :)
    • Firstly, many thanks for this script. I had been using a mysql script and it would not work when I converted it to mysqli. So this was a great place to start. Skynet is right. This script was closing the Channel tag right at the top, without an open one. This is my amended code (including an image and categories)...
      • 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
      $mysql_host = 'localhost'; //host $mysql_username = ''; //username $mysql_password = ''; //password $mysql_database = ''; //db date_default_timezone_set('Europe/London'); //set your SERVER timezone 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 $category = $channel->addChild('category', 'Health'); //add category node $category = $channel->addChild('category', 'Nursing'); //add category node $category = $channel->addChild('category', 'Care Homes'); //add category node $category = $channel->addChild('category', 'etc. etc. etc.'); //add category node $atom = $channel->addChild('atom:atom:link'); //add atom node $atom->addAttribute('href', 'URL to your page...'); //add atom node attribute $atom->addAttribute('rel', 'self'); $atom->addAttribute('type', 'application/rss+xml'); $title = $channel->addChild('title','Your title here...'); //title of the feed $description = $channel->addChild('description','Your description here...'); //feed description $link = $channel->addChild('link','URL to your page...'); //feed site $language = $channel->addChild('language','en-gb'); //language $atom = $channel->addChild('image'); //add an image (logo) $atom->addAttribute('title', 'Your image title here...'); $atom->addAttribute('url', 'Link to your image here...'); $atom->addAttribute('link', 'Your main domain name here...'); $atom->addAttribute('width', 'xxx'); //just numbers $atom->addAttribute('height', 'xxx'); //just numbers //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 = $channel->addChild('lastBuildDate',$date_f); //feed last build date $generator = $channel->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 **YOUR STUFF** [I selected Title, NURL, RSSDesc, NDate] FROM TABLE"); if($results){ //we have records while($row = $results->fetch_object()) { $item = $channel->addChild('item'); //add item node $title = $item->addChild('title', $row->Title); //add title node under item $link = $item->addChild('link', 'URL to your page.../'.$row->NURL.'/'); //add link node under item $guid = $item->addChild('guid', 'URL to your page.../'.$row->NURL.'/'); //add guid node under item $guid->addAttribute('isPermaLink', 'true'); //add guid node attribute - true or false $description = $item->addChild('description', $row->RSSDesc); //add description $date_rfc = gmdate(DATE_RFC2822, strtotime($row->NDate)); $item = $item->addChild('pubDate', $date_rfc); //add pubDate node } } echo $rss->asXML(); //output XML
      Cheers, G
New question is currently disabled!