1)组装字符串
2)使用系统类
- domdocument
- xmlwriter
- simplexml
这里以第一种方法举例:
$code, 'message' => $message, 'date' => $data ); echo json_encode($result); exit; } public static function xml(){ header(content-type:text/xml); $xml = \n; $xml .= \n; $xml .=200\n; $xml .=数据返回成功\n; $xml .=\n; $xml .=1\n; $xml .=ceshi\n; $xml .=\n; $xml .= \n; echo $xml; } 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 .=\n; $xml .= self::xmltoencode($result); echo $xml .=; } public static function xmltoencode($data){ $xml = $attr = ''; foreach($data as $key => $value){ if(is_numeric($key)){ $attr = id='{$key}'; $key = 'item'; } $xml .= ; $xml .= is_array($value)?self::xmltoencode($value):$value; //递归,如果value是数组,递归输出节点。 $xml .= {$key}>\n; } return $xml; }}$arr = array( 'id' => 1, 'name' => 'xxx', 'type' => array(4,5,6), 'test' => array(1,25,345=>array(123,'zifuchuan')));//40> 4//response::json(200,'数据返回成功',$arr);//response::xml();response::xmlencode(200,'success',$arr);
以上就介绍了封装通信接口数据的方法-xml,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。