PHP returns the JSON and XML classes, and returns the jsonxml_PHP tutorial.

Source: Internet
Author: User
PHP returns the JSON and XML Classes and jsonxml. PHP returns the JSON and XML classes, and jsonxml1? Php2classReponse {3 private $ resultarray (codenull, messagenull, datanull); 4 ** 5 * @ desc return JSON format 6 * @ parami PHP return JSON and XML Classes, return jsonxml
1
 Null ); 4/** 5 * @ desc return JSON format 6 * @ param int $ code 7 * @ param string $ message 8 * @ param array $ data 9 * return string10 */11 public static function json ($ code, $ message = null, $ data = array () {12 if (! Is_numeric ($ code) {13 return false; 14} 15 $ result = array (16 'code' => $ code, 17 'message' => $ message, 18 'data' => $ data19); 20 return json_encode ($ result); 21 exit; 22} 23 24/** 25 * @ desc return xml format data 26 * @ parma int $ code Status code 27 * @ param string $ message prompt 28 * @ param array $ data 29 * return string 30 */31 public static function xml ($ code, $ message = '', $ data = array () {32 if (! Is_numeric ($ code) {33 return false; 34} 35 $ result = array (36 'code' => $ code, 37 'message' => $ message, 38 'data' => $ data39); 40 $ xml = ''; 41 42 $ xml. ="
 \ N "; 43 $ xml. ="
 
  
\ N "; 44 $ xml. = self: xmlEncode ($ result); 45 $ xml. ="
 "; 46 header (" Content-Type: text/xml "); 47 echo $ xml; 48 49} 50 51 public static function xmlEncode ($ result) {52 $ xml = $ attr = ''; 53 foreach ($ result as $ key => $ val) {54 if (is_numeric ($ key )) {55 $ attr = "id = '{$ key}'"; 56 $ key = "item {$ key}"; 57} 58 $ xml. = "<{$ key} {$ attr}>"; 59 60 $ xml. = is_array ($ val )? Self: xmlEncode ($ val): $ val; 61 62 $ xml. ="
 \ N "; 63} 64 65 return $ xml; 66 67} 68 69} 70 71 $ data = array (72 'id' => 1, 73 'age' => 20, 74 'username' => 'Tim ', 75 'others '=> array (200, 3), 76); 77 Reponse: xml (200, 'success', $ data); 78 Reponse: json, 'success', $ data );

Limit 1? Php 2 class Reponse {3 // private $ result = array ('code' = null, 'message' = null, 'data' = null ); 4/** 5 * @ desc return the JSON format 6 * @ param I...

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.