DataReturn.class.php
<?php/** Return Data Format 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 '] [' retcode ']: ' Retcode ';//retcode name $format [' msg '] = $this->exists ($param, ' format.msg ')? $param [' Format '] [' msg ']: ' msg '; MSG is named $format [' data '] = $this->exists ($param, ' format.data ')? $param [' Format '] [' data ']: ' Data ';
The data pair 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 by 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 there are callback parameters, 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 XML format data in the Private function Xml_return () {header (' Content-type:text/xml;cha
Rset=utf-8 ');
echo $this->xml_encode ($this->returndata, $this->xmlroot); }//output JSON format data and adjust the 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\ ">\r\n"; echo $this->callback. " (". Json_encode ($this->returndata). "); \
r\n ";
echo "</script>"; //Output data format data//See more Highlights in this column: Http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/private func
tion Array_return () {header (' content-type:text/html;charset=utf-8 ');
Echo ' <pre> ';
Print_r ($this->returndata);
Echo ' </pre> '; //xml the password private function Xml_encode ($data, $root = ' XmlRoot ', $encoding = ' utf-8 ') {$XM L = "<?xml version=\" 1.0\ "encoding=\" ". $encoding.
"\" ">\n"; $xml. = "<". $root.
">\n";
$xml. = $this->data_to_xml ($data); $xml. = "</". $root.
">";
return $xml;
}//Digital 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; }//Data whether there is a 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); }//The need to add <! [cdata[]]> Tag Private Function CDATA ($val) {if!empty ($val) &&!preg_match ('/^[a-za-z0-9+$]/', $val)) {$val = ' <![
Cdata['. $val. ']]> ';
return $val; }//Class end?>
Demo