php编写app接口(二)-PHP生成XML数据
Jun 23, 2016 pm 01:24 PM
在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 就可以得到以下结果:
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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
