Search code examples
phpxmldynamic-datatemporary-files

Creating a temporary xml file with php


I am trying to create a temporary xml file with php that contains a list of quiz quesstions. The random list is generated from a database in php and I need to save this as a writable temporary xml file until the quiz is complete. I keep getting the following errors:

Warning: simplexml_load_file() [function.simplexml-load-file]: /tmp/quiz7YRGPo:1: parser error : Start tag expected, '<' not found in /home/dir/public_html/website.com/quiz/index.php on line 154

Warning: simplexml_load_file() [function.simplexml-load-file]: quiz_data.xml in /home/dir/public_html/website.com/quiz/index.php on line 154

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/dir/public_html/website.com/quiz/index.php on line 154

This is the code:

// name of XMLPHP file which contains quiz data

$xml_file = 'http://website.com/quiz/dynamic_php_page_generates_xml.php?param=var&param2=var2';


// create randomXML file

$tempquiz = tempnam('/tmp','quiz');
$fp_tempquiz = fopen($tempquiz, "w");
fwrite($fp_tempquiz, $xml_file);
//CLOSE temp file
fclose($fp_tempquiz);

// create new SimpleXML object from XML file

$quiz = simplexml_load_file($tempquiz);

How do I create the temporary xml file with php so that the quiz is unique each time the page is loaded?


Solution

  • $xml_file contains the URL of your file, not the actual XML content. You should read it first, with the file_get_contents function, for example :

    $xml_content = file_get_contents($xml_file);
    fwrite($fp_tempquiz, $xml_content);