PHP interface data format (json and xml)

Source: Internet
Author: User
The interface communication Data generated according to the MOOC's singwa tutorial does not have the *** generated interface data format * classResponse {*** [show outputs data in a comprehensive manner] * @ param [int] $ code [Status code] * @ param [string] $ message [prompt message] * @ paramarray $ data [data] * @ param [string] $ type

According to the generation interface communication data written by MOOC's singwa tutorial, there is no interface communication data/*** the interface data format is generated */class Response {/*** [show outputs data in a comprehensive manner] * @ param [int] $ code [Status code] * @ param [string] $ message [prompt message] * @ param array $ data [data] * @ param [string] $ type

Generate interface communication data according to MOOC's singwa tutorial <无>
/*** Generate interface data format */class Response {/*** [show outputs data in a comprehensive manner] * @ param [int] $ code [Status code] * @ param [string] $ message [prompt information] * @ param array $ data [data] * @ param [string] $ type [type] * @ return [string] [return Value] */ public static function show ($ code, $ message, $ data = array (), $ type = '') {if (! Is_numeric ($ code) {return '';} $ result = array ('code' => $ code, 'message' => $ message, 'data' => $ data); if ($ type = 'json') {return self: json ($ code, $ message, $ data );} elseif ($ type = 'xml') {return self: xml ($ code, $ message, $ data );} else {// TODO}/*** [json output data] * @ param [int] $ code [Status code] * @ param [string] $ message [prompt message] * @ param [array] $ data [data] * @ return [string] [return Value] */p Ublic static function json ($ code, $ message, $ data = array () {if (! Is_numeric ($ code) {return '';} $ result = array ('code' => $ code, 'message' => $ message, 'data' => $ data); $ result = json_encode ($ result); return $ result ;} /*** [xml generates data in xml format] * @ param [int] $ code [Status code] * @ param [string] $ message [prompt message] * @ param array $ data [data] * @ return [string] [return Value] */public static function xml ($ code, $ message, $ data = array () {if (! Is_numeric ($ code) {return '';} $ result = array ('code' => $ code, 'message' => $ message, 'data' => $ data); header ("Content-Type: text/xml"); $ xml ="
  \ N "; $ xml. ="
  
   
\ N "; $ xml. = self: xmlToEncode ($ data); $ xml. ="
  "; Return $ xml;} public static function xmlToEncode ($ data) {$ xml =''; foreach ($ data as $ key => $ value) {if (is_numeric ($ key) {$ attr = "id = '{$ key}'"; $ key = "item" ;}$ xml. = "<{$ key} {$ attr}> \ n"; $ xml. = is_array ($ value )? Self: xmlToEncode ($ value): "{$ value} \ n"; $ xml. ="
  \ N ";}return $ xml ;}// test $ grade = array (" score "=> array (70, 95, 70.0, 60," 70 "), "name" => array ("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "TianQi ")); $ response = new Response (); $ result = $ response: show (200, 'success ', $ grade, 'json'); print_r ($ result );

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.