Add method XML () in the reponse.php file;
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> Data return success </message>\n"; $xml. = "<data>\n"; $xml. = "<id>1</id>\n"; $xml. = "<name>singwa</name>\n"; $xml. = "</ Data>\n "; $xml. =" </root> "; echo $xml;}
XML () method analysis: 1, there are two ways to generate XML, one is to use the system API, there is a direct string concatenation. The method is to use string concatenation.
2, if the header ("Content-type:text/xml") is not added, then the following output format:
The header ("Content-type:text/xml") is added, and the XML format is fully output:
Special Note: You must ensure that the header ("Content-type:text/xml") is the first sentence of execution, UTF-8 the case of the added header ("Content-type:text/xml") will be an error, Because php file saving uft8 format is to output additional whitespace characters, the output XML stream of PHP files modified to ANSI format. The following is an error in the case of test.php for UTF-8 encoding:
3. Add code in the test.php file:
<?php require_once ('./response.php '); Response::xml ();? >
in the browser, enter:http://localhost/test.phpThe following results can be obtained:
PHP Authoring App Interface (ii)-php generating XML data