Log in

View Full Version : Create .xml file in php



zakmail007
05-25-2011, 06:39 AM
I want to save xml data in a separate file of .xml using cake php. i used the following code:

$a="test";
$b="test1";
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xml.= "<registrations>";
$xml.= " <registration>";
$xml.= " <id>".$a."</id>";
$xml.= " <name>".$b."</name>";
$xml.= " </registration>";

$xml.= "</registrations>";
$fh = fopen('my_xmls.xml', 'w+');
fwrite($fh, $xml);
fclose($fh);

What is the problem in my code or please give me a new code i'm using cake php.

djr33
05-25-2011, 02:35 PM
First, why are you sending headers if you are saving a file? Headers are sent directly to the browser to tell the browser what kind of data the server is sending-- you must pick one or the other: save to file or display in the browser. With XML, this means saving a .xml file on the server, or displaying/downloading a .xml document in the browser. If you do decide to save the file on the server, then you can do something else as output for the browser-- for example, you could say echo 'XML Saved!'; or you could even output the .xml directly: echo $xml; in this case in addition to saving it as a file and in that case you would need the headers. Does that make sense?

Second, your code looks fine for saving the file. Since you have the headers set to be sending the browser xml data, it may not be displaying any errors properly, so remove the headers and see what the output is. Also, turn on error reporting (including warnings, etc.). My best guess is that you don't have the correct permissions set to create that file. Everything else looks fine.



Also, I don't know much about cake php. Are you sure it allows all of this? Why not just use normal php?