Home > Backend Development > PHP Tutorial > php编写app接口(二)-PHP生成XML数据

php编写app接口(二)-PHP生成XML数据

WBOY
Release: 2016-06-23 13:24:52
Original
775 people have browsed it

在Reponse.php文件中添加方法xml();

public static function xml(){		header('Content-type: text/xml');		$xml = "<?xml version='1.0' encoding = 'UTF-8'?>\n";		$xml .="<root>\n";		$xml .="<code>200</code>\n";		$xml .="<message>数据返回成功</message>\n";		$xml .="<data>\n";		$xml .="<id>1</id>\n";		$xml .="<name>singwa</name>\n";		$xml .="</data>\n";		$xml .="</root>";				echo $xml;	}
Copy after login

xml()方法分析:1,生成XML有两种方法,一个是用系统API,还有一种是直接用字符串拼接。该方法就是用字符串进行拼接。
2,如果不添加header("Content-Type:text/xml");,则是下面的输出格式:



添加了header("Content-Type:text/xml");则会完整输出XML格式:



特别注意:必须保证header("Content-Type:text/xml")是执行的第一句,UTF-8的编码的情况下添加了header("Content-Type:text/xml")会报错,因为php文件保存uft8格式是会输出另外的空白字符,将输出xml流的php文件修改为ANSI格式的就好了。以下是test.php为UTF-8编码情况下的报错截图:



3,在test.php文件中添加代码:
<?php    require_once('./Response.php');    Response::xml();?>
Copy after login

浏览器中输入: http://localhost/test.php  就可以得到以下结果:

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template