array( 'index' => 'api' )
response.php
<?php
class Response
{
/**
* @param $code 状态码
* @param $message 提示信息
* @param array $data 数据
* return string
*/
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'?>";
$xml.="<root>";
$xml.=self::xmlToEncode($result);
$xml.="</root>";
echo $xml;
}
public static function xmlToEncode($data)
{
$xml = "";
foreach ($data as $key => $value) {
$xml .= "<{$key}>";
$xml .=is_array($value)?self::xmlToEncode($value):$value;
$xml .= "</{$key}>";
}
return $xml;
}
}
$data=array(
'id'=>1,
'name'=>'singwa',
);
Response::xmlEncode(200,'success',$data);
?>
array(3,4,5);
修改的代码
$xml = $attr="";
foreach ($data as $key => $value) {
if (is_numeric($key)){
$attr="id='{$key}'";
$key="item";
}
$xml .= "<{$key} {$attr}>";
'type'=>array(4,5,6),
'test'=>array(1,45,67=>array(123,'tsysa'),),
response.php
<?php
class Response
{
/**
* @param $code 状态码
* @param $message 提示信息
* @param array $data 数据
* return string
*/
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'?>";
$xml.="<root>";
$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}>";
}
return $xml;
}
}
$data=array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6),
'test'=>array(1,45,67=>array(123,'tsysa'),),
);
Response::xmlEncode(200,'success',$data);
?>