I want to output raw xml in a manner similar to http://www.google.com/ig/api?weather=Mountain+View but using PHP.
I have a very simple php script on my webserver:
<?php $output = "<root><name>sample_name</name></root>"; print ($output); ?>
All I can see in Chrome/firefox is "sample_name". I want to see:
<root> <name>sample_name</name> </root>
I cannot find a tutorial for anything THIS simple.
Thanks
php header("Content-type: text/xml"); $yourFile = "xmlfile. xml"; $file = file_get_contents($yourFile); echo $file; If you insist on simple xml you can write like this.
The contents from a URL can be fetched through the file_get_contents() and it can be echoed. or read using the readfile function. readfile('http://example.com/'); header('Content-type: text/xml'); //The correct MIME type has to be set before displaying the output. echo $xml->asXML(); or $xml->asXML('filename.
If you run the XML file as a PHP script, then yes.
By default PHP sets the Content-Type to text/html, so the browsers are displaying your XML document as an HTML page.
For the browser to treat the document as XML you have to set the content-type:
header('Content-Type: text/xml');
Do this before printing anything in your script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With