The DataReturn.class.php class files are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
<?php /** return Data format class * date: 2011-08-15 * author:fdipzone */ &nb sp; 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 '] [' retcode ']: ' Retcode ';//retcode corresponding name $format [' msg '] = $this-> Exists ($param, ' format.msg ')? $param [' Format '] [' msg ']: ' msg '; //msg corresponding name $format [' data '] = $this->exists ($param, ' Format.data ')? $param [' Format '] [' data ']: ' Data '; //data corresponding name $result = Array (); $result [$format [' retcode ']] = $this->exists ($param, ' Retcode ')? $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 callback parameter returns 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 XML Format data private function Xml_return () { header (' content-type:text/xml;charset=utf-8 '); echo $this->xml_encode ($this->returndata, $this->xmlroot); } //output JSON format data and invoke callback method private function Callback_return () { header (' Content-type:text/html;charset=utf-8 '); $this-> Callback = Empty ($this->callback)? ' Callback ': $this->callback; echo "<script type=" Text/javascript ">rn"; echo $this->callback. " (". Json_encode ($this->returndata)."); RN "; echo" </script> "; } //output Array Format data private function Array_return () { header (' Content-type:text/html;charset=utf-8 '); echo ' <pre> '; print_r ($this->returndata); echo ' </pre> '; } //xml code private function Xml_encode ($data,$root = ' XmlRoot ', $encoding = ' utf-8 ') { $xml = "<?xml version=" 1.0 "encoding=" ". $ Encoding. ">n"; $xml. = "<". $root. ">n"; $xml. = $this->data_to_xml ($data); $xml. = " </". $root. ">"; return $xml; } // Array conversion XML format private function data_to_xml ($data) { if (Is_object ($data)) { $data = Get_object_vars ($data); } $xml = '; foreach ($data as $key => $val) { is_numeric ($key) && $key = "Item id=" $key ""; $xml. = "< $key >"; $xml. = (Is_array ($val) | | is_objecT ($val))? $this->data_to_xml ($val): $this->cdata ($val); list ($key,) = Explode (', $key); $xml. = "</$key >n"; } return $xml; } // Determine whether the data exists private function exists ($obj, $key = ') { if ($key = = ') { return isset ($obj) &&!empty ($obj); }else{ $keys = Explode ('. ', $key); for ($i =0, $max =count ($keys); $i < $max; $i + +) { if (Isset ($obj [$keys [$i])) { $obj = $obj [$keys [$ i]]; }else{ return false; } } return isset ($obj) &&!empty ($ obj); } } //to determine whether the need to add <! [cdata[]]> tags private function CDATA ($val) { if (!empty ($val) & &!preg_match ('/^[a-za-z0-9+$]/', $val)) { $val = ' <![ Cdata['. $val. ']]> '; } return $val; } } //class end ?> |
The Demo sample program is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 |
<?php require_once (' DataReturn.class.php '); $param = Array (//DataReturn Parameters & nbsp ' type ' => ' JSON ',//output types Json,xml,callback, ARRAY defaults to json ' retcode ' => ' 1000 ',//Retcode values , the default is 0 ' msg ' => ', / MSG value, default to null ' data ' => Array (///to Output ' id ' => ', ' name ' => ' fdipzone ', ' Gender ' => 1, ' age ' => 28 ), ' format ' => array (//Output Data key format, default to retcode,msg,data ' Retcode ' => ' status ', ' msg ' => ' info ', ' Data ' => ' result ' ), ' xmlroot ' => ' XmlrOot ',//when type=xml, XML root node name, default xmlroot ' Callback ' => ' callback '/* callback method name When type=json, the default is null, if not empty, The output callback ({data}); When type=callback, the default is callback, Automatically call page JS callback method */ ); $obj = new DataReturn ($ param); Create DataReturn class objects $obj->data_return (); //output data in format ?> |