<?php/* Based on the RSS 2.0 specification available at: http://cyber.law.harvard.edu/rss/rss.html *//* RSS Test Exercise v0.1 *//* (c) Thomas Cameron -07 */// open the rss classclass rss { // create the rss feed function feed_make($channel_vars, $item_vars) { // create an array of all the possible channel elements // req. elements = title, link, description $channel_elements = array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "category", "generator", "docs", "cloud", "ttl", "image", "rating", "textInput", "skipHours", "skipDays"); // create an array of all the possible item elements // req. elements = either title or description MUST be present, all others optional $item_elements = array("title", "link", "description", "author", "category", "comments", "enclosure", "guid", "pubDate", "source"); // open the feed with the xml and rss version statements $final_feed = "<?xml version=\"1.0\" ?>\n<rss version=\"2.0\">\n<channel>\n"; // cycle through each item in $channel_elements foreach($channel_elements as $element) { // if the current element is present in $channel_vars lets process it if(!empty($channel_vars['".$element."'])) { // add the element and value into the rss feed <channel> section $final_feed .= "<".$element.">".$item_vars['".$element."']."</".$element.">\n"; } } // $item_vars may contain more than one item, lets do all of them for($i=0,$i<count($item_vars),$i++) { // open the item section $final_feed .= "<item>\n"; // cycle through each item in $item_elements foreach($item_elements as $element) { // a minimum of either a title or description is required, if neither is set give it a title if(empty($item_vars[$i]['title']) && empty($item_vars[$i]['description'])) { // we'll just put Untitled here for safety $final_feed .= "<title>Untitled</title>"; } // if the current element is present in $item_vars lets process it if(!empty($item_vars[$i]['".$element."'])) { // add the element and value into the rss feed current item section $final_feed .= "<".$element.">".$item_vars[$i]['".$element."']."</".$element.">\n"; } } // close the item section $final_feed .= "</item>\n"; } // close the channel section and also the feed $final_feed .= "</channel>\n</rss>"; // return what we've just done to the user return $final_feed; } // output the feed to browser (or file) function output($feed, $file=null,$path=null) // if $file and $path are specified we need to write to a file if(!empty($file) || !empty($path)) { // use $fh as a handle and open the file for writing $fh = fopen($path."/".$file, "w") or die("Could not open file"); // write our feed into the $fh handle fwrite($fh, $feed) or die("Could not write to file."); // now lets close the file to save it fclose($fh); // it's not specified, let's output put it to the browser } else { // send an xml content-type to the browser so they know header("Content-type: text/xml"); // echo the feed to the browser echo $feed; } }}/* sample code START */// make a new instantation of the class$rss = new rss;// give the values to the feed maker$feed = $rss->feed_make($channel_vars, $item_vars);// output the generated feed to the client$rss->output($feed);/* sample code END */?>