Php implementation of the returned data formatting class instance

Source: Internet
Author: User
This article mainly introduces the returned data formatting class implemented by php and its application examples, including formatting for XML and JSON, which is very useful, for more information about how to format returned data in php and how to use it, see the example in this article. it is of great practical value in string processing. Share it with you for your reference. The specific method is as follows:

The DataReturn. class. php class file is as follows:

<? Php/** return data formatting class * Date: 2011-08-15 * Author: fdipzone */class DataReturn {// class start private $ type; private $ xmlroot; private $ callback; private $ returnData; public function _ construct ($ param = array () {$ this-> type = $ this-> exists ($ param, 'type ')? Strtoupper ($ param ['type']): 'json'; // type JSON, XML, CALLBACK, ARRAY $ this-> xmlroot = $ this-> exists ($ param, 'xmlroot ')? $ Param ['xmlroot']: 'xmlroot'; // xml root dom name $ this-> callback = $ this-> exists ($ param, 'callback ')? $ Param ['callback']: ''; // JS callback function name $ format = array (); $ format ['retcode'] = $ this-> exists ($ param, 'format. retcode ')? $ Param ['format'] ['refercode']: 'refercode'; // retcode corresponding name $ format ['MSG '] = $ this-> exists ($ param, 'format. msg ')? $ Param ['format'] ['MSG ']: 'MSG'; // msg name $ format ['data'] = $ this-> exists ($ param, 'format. data ')? $ Param ['format'] ['data']: 'data'; // the corresponding data name $ result = array (); $ result [$ format ['refercode'] = $ this-> exists ($ param, 'refercode ')? $ Param ['retcode']: 0; $ result [$ format ['MSG '] = $ this-> exists ($ param, 'MSG ')? $ Param ['MSG ']: ''; $ result [$ format ['data'] = $ this-> exists ($ param, 'data ')? $ Param ['data']: ''; $ this-> returnData = $ result;} // output data public function data_return () {ob_clean (); switch ($ this-> type) {case 'json': $ this-> json_return (); break; case 'xml': $ this-> xml_return (); break; case 'callback': $ this-> callback_return (); break; case 'array': $ this-> array_return (); break; default: $ this-> json_return ();} exit ();} // output JSON format data. if the callback parameter exists, return the JSONP format private function json _ Return () {header ('content-type: text/html; charset = utf-8 '); if (empty ($ this-> callback )) {echo json_encode ($ this-> returnData);} else {echo $ this-> callback. '('. json_encode ($ this-> returnData ). ');' ;}}// output data in XML format private function xml_return () {header ('content-type: text/xml; charset = utf-8 '); echo $ this-> xml_encode ($ this-> returnData, $ this-> xmlroot);} // output JSON data and call the callback method private function callback_r Eturn () {header ('content-type: text/html; charset = utf-8 '); $ this-> callback = empty ($ this-> callback )? 'Callback': $ this-> callback; echo"

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.