Php app development interface (1) php returns the result to the app in json or xml format. To understand this, we can easily say that the data is packaged into json or xml and returned to the APP.
Define the abstract APP base class:
Return to the APP in xml format:
$ Code, 'message' => $ message, 'data' => $ data); header ('content-Type: text/XML'); $ xml ="
\ N "; $ xml. ="
"; $ Xml. = self: xmlToEncode ($ result); $ xml. ="
"; Echo $ xml;} public static function xmlToEncode ($ result) {$ xml = $ attr =''; foreach ($ result as $ key => $ value) {// judge the key-value pair. if it is a numeric key value, if (is_numeric ($ key) {$ attr = "id = '". $ key. "'"; $ key = "item";} $ xml. = "<{$ key} {$ attr}>"; // returns recursively because the array is displayed as an array in xml, the specific key-value pair $ xml must be displayed. = is_array ($ value )? Self: xmlToEncode ($ value): $ value; $ xml. ="
\ N ";}return $ xml ;}}
Return data in json format:
$ Code, 'message' => $ message, 'data' => $ data); echo json_encode ($ result); exit ;}}
You can also assemble the returned data in this way:
$ Code, 'message' => $ message, 'data' => $ data,); if ($ type = 'json') {self: json ($ code, $ message, $ data); exit;} elseif ($ type = 'array') {// suitable for debugging code var_dump ($ result );} elseif ($ type = 'xml') {self: xmlEncode ($ code, $ message, $ data); exit ;} else {// TODO}/*** output communication data in json format * @ param integer $ code Status code * @ param string $ message prompt message * @ param array $ data * return string */public static function json ($ code, $ me Ssage = '', $ data = array () {if (! Is_numeric ($ code) {return '';} $ result = array ('code' => $ code, 'message' => $ message, 'data' => $ data); echo json_encode ($ result); exit ;} /*** output communication data in xml format * @ param integer $ code Status code * @ param string $ message prompt message * @ param array $ data * return string */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 ="
\ N "; $ xml. ="
\ N "; $ xml. = self: xmlToEncode ($ result); $ xml. ="
"; 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; $ xml. ="
\ N ";}return $ xml ;}}