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
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