<?php
class Response{
/**
*按josn方式输出通信数据
*/
public static function json($code,$message='',$data = array()){
if(!is_numeric($code)){
return '';
}
$result = array(
'code' =>$code,
'message'=>$message,
'data'=>$data
);
echo json_encode($result,JSON_UNESCAPED_UNICODE);
exit;
}
public static function xmlEncode($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.="<root>\n";
$xml.=self::xmlToEncode($result);
$xml.="</root>";
echo $xml;
}
public static function xmlToEncode($data){
$xml =$attr="";
foreach($data as $key => $value){
if(is_numeric($key)){
$attr="id={$key}";
$key="item";
}
$xml.="<{$key}{$attr}>";
$xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="</{$key}>\n";
}
return $xml;
}
}
$data = array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6)
);
Response::xmlEncode(200,'success',$data);