<?php class Response_xml{ /** *按xml方式输出通信 *@param integet $code 状态码 *@param string $message 提示信息 *@param array $data 数据 * return string */ public static function xml_encoding($code,$message,$data=array()){ if(is_null($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::xml_array($result); $xml.="</root> "; echo $xml; } public static function xml_array($result){ $xml=$attr=""; foreach ($result as $key => $value) { if(is_numeric($key)){ $attr=" id='{$key}'"; $key="item"; } $xml.="<{$key}{$attr}> "; $xml.=is_array($value)?self::xml_array($value):$value; $xml.="</{$key}> "; } return $xml; } } $data=array( 'id'=>1, 'name'=>'hgj123', ); $data1=array(1,2,3,11,122); Response_xml::xml_encoding(200,'数据返回成功',$data1);