I have a PHP page that outputs XML. But for efficiency reasons, it's probably best to serve an XML page. Right?
The XML is podcast RSS data. http://kansaspublicradio.org/widgets/podcasts/retro-cocktail-hour.php and there's one more just like it for a different show. The PHP code gathers the data from our CMS (drupal) and puts it in the itunes specific XML tags. I found this to be a better solution than any available drupal module.
But it's a little unconventional to serve XML with a PHP file. And that PHP code will be executed anytime anyone, or anything i.e. itunes or feedburner, makes a request for the RSS data. The PHP code just queries the database then loops through the results to write the XML tags, but there's still an inherent memory and sql performance hit every time.
So I need to serve a static XML file. How can I save the output of the PHP to a file??
I imagine I'll create another PHP script, like makepodcast.php, that runs the original PHP page but prints it's output to an XML page. Then I can make a cron job that does this once a week (the show is recorded weekly).
But what I'm lost on is just how exactly do I save the output of a PHP and write it to another file??
You have to organise your XML's Build : per period, per signal, per event ? Once it is done, you can write something like :
<?php
header("Content-type: text/xml");
define ('XML_FILE_PATH', 'CUSTOMIZE/YOUR/XML/FILE/PATH');
if ($_MUST_BUILD_XML) {
// put here your old PHP code that it will build your XML file
// i guess that it is something like $xml = '<xml>
// <videogame>
// <ps4>The Best One :) </ps4>
// </videogame>'
buildXmlFile('built_file.xml', $xml);
print $xml;
}
else {
print readXmlFile('built_file.xml');
}
/**
* @return string : built XML data
* @param string : XML file path and name
* @throws Exception
*/
function buildXmlFile($data_s, $file_name_s) {
$fp = fopen ("XML_FILE_PATH/" . $file_name_s , "w");
if(!$fp) {
/*TODO Error*/ throw new Exception('...');
}
fputs($f, $data_s);
fclose ($f);
}
/**
* @param string : XML file path and name
* @return string : built XML data
* @throws Exception
*/
function readXmlFile($file_name_s) {
$fp = fopen ("XML_FILE_PATH/" . $file_name_s , "r");
if(!$fp) {
/*TODO Error*/ throw new Exception('...');
}
$contents = fread($fp, filesize("XML_FILE_PATH/" . $file_name_s);
fclose ($f);
return ($contents);
}
Hope that helpls :)