PHP: Write to file only after the variable contains data from the XML -


i'm new php. i'm trying to:

  1. write data xml file variable;
  2. write data variable file;

the file empty. think has delay during xml file parsing. variable created, empty , that's why file empty. there delay or sort of workaround this?

the code:

$xml = simplexml_load_file("somexmlfilewhichtakestimetoload.xml") or die ("error: cannot create object"); $myfile = fopen("eefuelprice.txt", "w") or die("unable open file!"); print_r($xml); fwrite($myfile, $xml); print_r("successfully wrote file"); 

thanks in advance!

i'm assuming want work xml xml, rather string ...

note: simplexml_load_file() blocking function: when returns, file has parsed.

you need error handling: you're printing $xml assume prints data you're expecting. fwrite() returns number of bytes written -- return? if it's zero, (which be) issue.

you instead use asxml() print xml object file. fwrite() writing bytes (strings) file.


Comments