Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

Output raw XML using php

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...