<?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‘?>\n"; $xml.="<root>\n"; $xml.=self::xml_array($result); $xml.="</root>\n"; 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}>\n"; $xml.=is_array($value)?self::xml_array($value):$value; $xml.="</{$key}>\n"; } return $xml; } } $data=array( ‘id‘=>1, ‘name‘=>‘hgj123‘, ); $data1=array(1,2,3,11,122); Response_xml::xml_encoding(200,‘数据返回成功‘,$data1);
时间: 2024-10-29 03:44:48