Texan78 Posted June 12, 2013 Share Posted June 12, 2013 I know what the topic says and I know some people will say "Did you google it"? I know XML is the language in which RSS feeds are created but what I am trying to do is unique. I have a HTML form that submits data in which that data is used to create an XML file that I use to display data in tables. I chose this route because it's a lot easier and lightweight for what I was needing versus using a database. Now what I am trying to do is create a RSS from that XML file that I parse to display data. Is this even possible and what would be the best approach to do this? When I Google it all I can find is information regarding creating and RSS feed from a Database. Which I am trying to avoid that route. So is it possible to create a script that will read that XML file then format it as an RSS feed? If so can someone point me to some examples or tutorials? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/ Share on other sites More sharing options...
Irate Posted June 12, 2013 Share Posted June 12, 2013 SimpleXML parses XML documents into PHP-readable files out of which you can create RSS feeds. Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435507 Share on other sites More sharing options...
Texan78 Posted June 12, 2013 Author Share Posted June 12, 2013 (edited) SimpleXML parses XML documents into PHP-readable files out of which you can create RSS feeds. Correct, I understand I can use SimpleXML to parse it as that is what I planned to do as the current XML file is parsed with SimpleXML to display the data on a HTML page. What I am a little confused on is the required elements for RSS are not the same as the elements in the XML file. I have been unable to locate anything that describes how to read and XML file and format it to an RSS feed. So it needs to read the XML file then format it as RSS then save it as RSS. This is the php script I use to create the XML file from my HTML form. <?php if (isset($_POST['lsr-submit'])) { header('Location: http://www.mesquiteweather.net/wxmesqLSR.php'); } $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>'; $xml = false;if(file_exists('mesquite.xml')) $xml = simplexml_load_file('mesquite.xml'); if(!$xml) $xml = simplexml_load_string('<entrys/>'); $name = $_POST['name']; $location = $_POST['location']; $report = $_POST['report']; $description = $_POST['desc']; $fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false); $lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false); $location = htmlentities($location, ENT_COMPAT, 'UTF-8', false); $report = htmlentities($report, ENT_COMPAT, 'UTF-8', false); $description = htmlentities($description, ENT_COMPAT, 'UTF-8', false); $reports = $xml->addChild('reports'); $reports->addChild('timestamp', date("D M jS g:i a",time())); $reports->addChild('name', $name); $reports->addChild('location', $location); $reports->addChild('report', $report); $reports->addChild('description', $description); $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $doc->preserveWhiteSpace = true; $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS); $doc->save('mesquite.xml'); ?> So does anyone have any suggestions or can point me in the direction of a tutorial that describes how to read an XML file to create an XML feed? I think I am making this hard than it is which I know should be a really simple process. -Thanks Edited June 12, 2013 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435598 Share on other sites More sharing options...
Texan78 Posted June 13, 2013 Author Share Posted June 13, 2013 So I have been trying some things and I almost have this. It's very ugly as I really can't find anything related to exactly what I am trying to do. So I am sure there is probably an cleaner way to do this. This is the code I have now that creates an rss feed. Only two problems with it. 1. It only displays one entry from the XML file. 2. It doesn't save it as a new file I.E. in this case mesquite.rss. Now I can call the script directly in the browser and it looks right with the exception of the two problems above. http://www.mesquiteweather.net/xml/process_copy1.php Here is the code to produce this. <?php if (file_exists('mesquite.xml')) { $xml = simplexml_load_file('mesquite.xml'); foreach($xml->reports as $reports){ $name = $reports->name; $location = $reports->location; $report = $reports->report; $description = $reports->description; $timestamp = $reports->timestamp; } echo "<?xml version='1.0' encoding='iso-8859-1'?> <rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom'> <!-- version 1.00 - 10-Jun-2012 --> <channel> <title>Mesquite Area Storm Reports</title> <link>/</link> <description>Courtesy of Mesquite Weather</description> <language>en-us</language> <generator>RSS feed version 1.00 - 10-Jun-2012</generator> <copyright>Mesquite Weather</copyright> <pubDate>2013-06-12T14:54:05 CDT</pubDate> <ttl>15</ttl> <lastBuildDate>2013-06-12T14:54:05 CDT</lastBuildDate> <image> <url>http://www.mesquiteweather.net/images/App_75_x_75.png</url> <width>75</width> <height>75</height> </image>"; echo "<item> <title>$report</title> <link>http://www.mesquiteweather.net/wxmesqLSR.php</link> <description>Location: $location - $timestamp - Reported by: $name - Description: $description</description> </item>"; } echo "</channel></rss>"; $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $doc->preserveWhiteSpace = true; $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS); $doc->save('mesquite.rss'); ?> Any ideas or suggestions? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435646 Share on other sites More sharing options...
Texan78 Posted June 13, 2013 Author Share Posted June 13, 2013 Ok I have solved one of my issues and I am able to display multiple entries now. The only issue I have is it doesn't save it as a new file, in this case mesquite.rss the I have to call the actually script in order to view the rss feed. <?php if (file_exists('mesquite.xml')) { $xml = simplexml_load_file('mesquite.xml'); echo "<?xml version='1.0' encoding='iso-8859-1'?>"; echo "<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom'>"; echo "<!-- version 1.00 - 10-Jun-2012 -->"; echo "<channel>"; echo "<title>Mesquite Area Storm Reports</title>"; echo "<link>/</link>"; echo "<description>Courtesy of Mesquite Weather</description>"; echo "<language>en-us</language>"; echo "<generator>RSS feed version 1.00 - 10-Jun-2012</generator>"; echo "<copyright>Mesquite Weather</copyright>"; echo "<pubDate>2013-06-12T14:54:05 CDT</pubDate>"; echo "<ttl>15</ttl>"; echo "<lastBuildDate>2013-06-12T14:54:05 CDT</lastBuildDate>"; echo "<image>"; echo "<url>http://www.mesquiteweather.net/images/App_75_x_75.png</url>"; echo "<width>75</width>"; echo "<height>75</height>"; echo "</image>"; foreach($xml->reports as $reports){ $name = $reports->name; $location = $reports->location; $report = $reports->report; $description = $reports->description; $timestamp = $reports->timestamp; echo "<item>\n"; echo "<title>$report</title>\n"; echo "<link>http://www.mesquiteweather.net/wxmesqLSR.php</link>\n"; echo "<description>Location: $location - $timestamp - Reported by: $name - Description: $description</description>\n"; echo "</item>\n"; } } echo "</channel></rss>"; $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $doc->preserveWhiteSpace = true; $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS); $doc->save('mesquite.rss'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435653 Share on other sites More sharing options...
Texan78 Posted June 13, 2013 Author Share Posted June 13, 2013 It wouldn't let me edit my last post. So here is the current code I have and it seems to be working great. Only problem is it's not saving to a new file which should be mesquite.rss that is defined at the bottom. What am I missing or doing wrong? -Thanks <?php // Load the XML file to read and parse if (file_exists('mesquite.xml')) { $xml = simplexml_load_file('mesquite.xml'); // grab current date/time $pubTime = date("c T"); // Show the RSS header information echo "<?xml version='1.0' encoding='iso-8859-1'?>"; echo "<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom'>"; echo "<!-- version 1.00 - 10-Jun-2012 -->"; echo "<channel>"; echo "<title>Mesquite Area Storm Reports</title>"; echo "<description>Courtesy of Mesquite Weather</description>"; echo "<language>en-us</language>"; echo "<copyright>Mesquite Weather</copyright>"; echo "<pubDate>$pubTime</pubDate>"; echo "<ttl>15</ttl>"; echo "<lastBuildDate>$pubTime</lastBuildDate>"; echo "<image>"; echo "<url>http://www.mesquiteweather.net/images/App_75_x_75.png</url>"; echo "<width>75</width>"; echo "<height>75</height>"; echo "</image>"; // Parse the XML and set the variables foreach($xml->reports as $reports){ $name = $reports->name; $location = $reports->location; $report = $reports->report; $description = $reports->description; $timestamp = $reports->timestamp; // Display the RSS entries echo "<item>\n"; echo "<title>$report</title>\n"; echo "<pubDate>$timestamp</pubDate>"; echo "<link>http://www.mesquiteweather.net/wxmesqLSR.php</link>\n"; echo "<description>Location: $location - Reported by: $name - Description: $description</description>\n"; echo "</item>\n"; } } echo "</channel></rss>"; // Save RSS File $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $doc->preserveWhiteSpace = true; $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS); $doc->save('mesquite.rss'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435663 Share on other sites More sharing options...
Solution Texan78 Posted June 13, 2013 Author Solution Share Posted June 13, 2013 (edited) I got it figured out. Here is what I did in case anyone is looking for something like this. <?php // Load the XML file to read and parse if (file_exists('mesquite.xml')) { $xml = simplexml_load_file('mesquite.xml'); // grab current date/time $pubTime = date("c T"); // Show the RSS header information $rss_output .= "<?xml version='1.0' encoding='iso-8859-1'?>\n"; $rss_output .= "<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom'>\n"; $rss_output .= "<!-- version 1.00 - 13-Jun-2013 -->"; $rss_output .= "<channel>\n"; $rss_output .= "<title>Mesquite Area Storm Reports</title>\n"; $rss_output .= "<description>Courtesy of Mesquite Weather</description>\n"; $rss_output .= "<language>en-us</language>\n"; $rss_output .= "<copyright>Mesquite Weather</copyright>\n"; $rss_output .= "<pubDate>$pubTime</pubDate>\n"; $rss_output .= "<ttl>15</ttl>\n"; $rss_output .= "<lastBuildDate>$pubTime</lastBuildDate>\n"; $rss_output .= "<image>\n"; $rss_output .= "<url>http://www.mesquiteweather.net/images/App_75_x_75.png</url>\n"; $rss_output .= "<width>75</width>\n"; $rss_output .= "<height>75</height>\n"; $rss_output .= "</image>\n"; // Parse the XML and set the variables foreach($xml->reports as $reports){ $name = $reports->name; $location = $reports->location; $report = $reports->report; $description = $reports->description; $timestamp = $reports->timestamp; // Display the RSS entries $rss_output .= "<item>\n"; $rss_output .= "<title>$report</title>\n"; $rss_output .= "<pubDate>$timestamp</pubDate>"; $rss_output .= "<link>http://www.mesquiteweather.net/wxmesqLSR.php</link>\n"; $rss_output .= "<description>Location: $location - Reported by: $name - Description: $description</description>\n"; $rss_output .= "</item>\n"; } } $rss_output .= "</channel>\n"; $rss_output .= "</rss>"; // Save RSS File file_put_contents('mesquite.rss', $rss_output); ?> Edited June 13, 2013 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/279061-creating-rss-feed/#findComment-1435701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.