Json and xml interface data formats generated by PHP. jsonxml_PHP tutorial

Source: Internet
Author: User
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...

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.