时间:2021-07-01 10:21:17 帮助过:17人阅读
1)组装字符串
2)使用系统类
- DomDocument
- XMLWriter
- SimpleXML
这里以第一种方法举例:
输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function json($code, $message = '', $data = array()) { if(!is_numeric($code)){ return ''; } $result = array( 'code' => $code, 'message' => $message, 'date' => $data ); echo json_encode($result); exit; } public static function xml(){ header("content-type:text/xml"); $xml = "\n"; $xml .= "输出节点。 $xml .= "{$key}>\n"; } return $xml; } } $arr = array( 'id' => 1, 'name' => 'xxx', 'type' => array(4,5,6), 'test' => array(1,25,345=>array(123,'zifuchuan')) ); //<0>40>\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 .="200
\n"; $xml .="数据返回成功 \n"; $xml .="\n"; $xml .="1 \n"; $xml .="ceshi \n"; $xml .="\n"; $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 .= "<{$key}{$attr}>"; $xml .= is_array($value)?self::xmlToEncode($value):$value; //递归,如果value是数组,递归
以上就介绍了封装通信接口数据的方法-xml,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。