Php for Android server development-json xml universal app Data Communication Interface encapsulated by php
Php file: test. php is a common communication class
$ Code, 'message' => $ message, 'data' => $ data,); if ($ type = 'json') {self: json ($ code, $ message, $ data); exit;} elseif ($ type = 'array') {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, $ message =' ', $ 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 =
; $ Xml. =
; $ 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. =
;} Return $ xml ;}}
Testxml & json. php generates xml and json by calling the show () method of the test class.
1,'name'=>david);Response::show(200,'success',$data);