使用 php 输出原始 XML [英] Output raw XML using php

查看:29
本文介绍了使用 php 输出原始 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以类似于 http://www.google.com/的方式输出原始 xmlig/api?weather=Mountain+View 但使用 PHP.

I want to output raw xml in a manner similar to http://www.google.com/ig/api?weather=Mountain+View but using PHP.

我的网络服务器上有一个非常简单的 php 脚本:

I have a very simple php script on my webserver:

<?php 

      $output = "<root><name>sample_name</name></root>";
      print ($output);
?> 

我在 Chrome/firefox 中只能看到sample_name".我想看:

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.

谢谢

推荐答案

默认情况下,PHP 将 Content-Type 设置为 text/html,因此浏览器将您的 XML 文档显示为 HTML 页面.

By default PHP sets the Content-Type to text/html, so the browsers are displaying your XML document as an HTML page.

为了让浏览器将文档视为 XML,您必须设置内容类型:

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.

这篇关于使用 php 输出原始 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆