Json and xml interface data format generated by PHP, jsonxml. PHP generates json and xml interface data formats, jsonxmlphp generates interface communication data ** generate interface data format * classResponse {*** [show outputs data in a comprehensive manner] * @ param PHP generate json and xml interface data formats, jsonxml
Php generates interface communication data
/*** 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 = "<? Xml version = '1. 0' encoding = 'utf-8'?> \ 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 );
The above is all the content of this article. I hope you will like it.
Php generates interface communication data/*** generates interface data format */class Response {/*** [show outputs data in a comprehensive manner] * @ param...