Php encapsulation of jsonxml universal app data communication interface for Android server developed by php

Source: Internet
Author: User
Php for Android server development php encapsulation jsonxml common app data communication interface 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 ="
 \ 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 ;}}

Testxml & json. php generates xml and json by calling the show () method of the test class.


 1,'name'=>"david");Response::show(200,'success',$data);


In this way, app developers can assign different values to the last parameter of the show () function to select whether to generate xml data or json data.

Parameters of the get request through the http request:

Http: // localhost/testxml & json. php? Format = xml this is the url for generating xml

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.