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

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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles