Simple way to create an rss feed using php5
<?php
//create xml doc
$doc = new DOMDocument("1.0");
//create the rss element
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$root->setAttribute('version','2.0');
//create the channel element
$channel = $doc->createElement("channel");
$channel = $root->appendChild($channel);
$elements = array();
$elements["title"] = "my feed's title";
$elements["link"] = "http://mydomain.com/index.rss";
$elements["description"] = "Demonstration feed created using PHP5 and the DOM extension. The php script that generated this document can be found at : http://thousandrobots.com/dev/xml/dom/auto.phps";
$elements["language"] = "en-us";
$elements["copyright"] = "copyright 2005 thousandrobots.com";
$elements["docs"] = "http://backend.userland.com/rss";
$elements["generator"] = "thousand robots dom-based rss generator";
$elements["managingEditor"] = "editor@spam.thousandrob0ts.com (ADM)";
$elements["webMaster"] = "webmaster@spam.thousandrob0ts.com (ADM)";
$elements["lastBuildDate"] = "Sat, 05 Feb 2005 23:39:07 EST";
foreach ($elements as $elementname => $elementvalue)
{
$elementname = $doc->createElement($elementname);
$elementname = $channel->appendChild($elementname);
$elementname->appendChild($doc->createTextNode($elementvalue));
}
//clear the array
unset($elements);
//now create the first feed item
//instead, you could just pull some items from a db into an elements[] array.
$elements[0]["title"] = "title of first item";
$elements[0]["description"] = "a short description of the first item";
$elements[0]["link"] = "http://thousandrobots.com/?article=1";
$elements[0]["author"] = "author@spam.thousandrob0ts.com (ADM)";
$elements[0]["pubDate"] = "Sat, 05 Feb 2005 23:39:07 EST";
$elements[0]["category"] = "tech";
$elements[0]["guid"] = "http://thousandrobots.com/?article=1";
//create the other items
$elements[1]["title"] = "title of next item";
$elements[1]["description"] = "a short description of the next item";
$elements[1]["link"] = "http://thousandrobots.com/?article=N";
$elements[1]["author"] = "author@spam.thousandrob0ts.com (ADM)";
$elements[1]["pubDate"] = "Sat, 05 Feb 2005 22:39:07 EST";
$elements[1]["category"] = "tech";
$elements[1]["guid"] = "http://thousandrobots.com/?article=N";
//loop through each item and add its elements to the tree
foreach ( $elements as $element )
{
//create the item element
$item = $doc->createElement("item");
$item = $channel->appendChild($item);
foreach ( $element as $elementname => $elementvalue )
{
$elementname = $doc->createElement($elementname);
$elementname = $item->appendChild($elementname);
$elementname->appendChild($doc->createTextNode($elementvalue));
}
}
//output the xml
header('Content-Type: text/xml');
echo $doc->saveXML();
?>