class?Response{
/**
?*?@param?integer?$code?狀態(tài)碼
?*?@param?string?$message?提示信息
?*?@param?array?$data?數(shù)據(jù)??
?*?返回?string(json)
?*/
public?static?function?jsonData($code,$message,$data?=?array()){
//如果狀態(tài)碼不是數(shù)字直接返回null
if(!is_numeric($code)){
return?'not?a?num';
}
//封裝數(shù)據(jù)
$result?=?array(
'code'?=>?$code,
'message'?=>?$message,
'data'?=>?$data
);
echo?json_encode($result);
}
/**
?*?@param?integer?$code?狀態(tài)碼
?*?@param?string?$message?提示信息
?*?@param?array?$data?數(shù)據(jù)
?*?返回?string(xml)
?*/
public?static?function??xmlData($code,?$message,$data?=?array()){
if(!is_numeric($code)){
return?'';
}
//封裝array數(shù)據(jù)
$result?=?array(
'code'=>$code,
'message'=>$message,
'data'=>$data,
);
//封裝xml數(shù)據(jù)
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="";
foreach($data?as?$key=>$value){
$xml.="<{$key}>";
$xml.="$value";
$xml.="</{$key}>\n";
}
return?$xml;
}
}
$data?=?array(
'id'=>1,
'name'=>'mike',
'num'=>123,
);
Response::xmlData(200,?"ggood",$data);
?>
添加回答
舉報
0/150
提交
取消