Method for encapsulating communication interface data-xml

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.