: This article mainly introduces the method of encapsulating communication interface data-xml. For more information about PHP tutorials, see. Php generates XML data
1) assemble strings
2) use the system class
-DomDocument
-XMLWriter
-SimpleXML
The first method is used as an example:
$ Code, 'message' => $ message, 'date' => $ data); echo json_encode ($ result); exit;} public static function xml () {header ("content-type: text/xml"); $ xml ="
\ N "; $ xml. ="
\ N "; $ xml. ="
200
\ N "; $ xml. ="
Data returned successfully
\ N "; $ xml. ="
\ N "; $ xml. ="
1
\ N "; $ xml. ="
Ceshi
\ N "; $ xml. ="\ N "; $ xml. ="
\ N "; echo $ xml;} public static function xmlEncode ($ code, $ message, $ data = array () {if (! Is_numeric ($ code) {return;} $ result = array ('code' => $ code, 'message' => $ message, 'data' => $ data ); header ("Content-Type: text/xml"); $ xml ="
"; $ Xml. ="
\ N "; $ xml. = self: xmlToEncode ($ result); echo $ xml. ="
";} Public static function xmlToEncode ($ data) {$ xml = $ attr =''; foreach ($ data as $ key => $ value) {if (is_numeric ($ key) {$ attr = "id = '{$ key}'"; $ key = 'ITEM' ;}$ xml. = "<{$ key} {$ attr}>"; $ xml. = is_array ($ value )? Self: xmlToEncode ($ value): $ value; // recursion. if value is an array, recursive output nodes are generated. $ Xml. ="
\ N ";}return $ xml ;}$ arr = array ('id' => 1, 'name' => 'XXX ', 'type' => array (345, 6), 'test' => array (123, => array (, 'zifuchuan '))); // <0> 4
4
// Response: json (200, 'data returned successfully', $ arr); // Response: xml (); Response: xmlEncode (200, 'success ', $ arr );
The above describes the method of encapsulating communication interface data-xml, including the content, hope to be helpful to friends who are interested in PHP tutorials.