第2章 封装通讯接口方法php
一、JSON方式封装接口数据方法json
PHP生成JSON数据数组
方法:json_encode($value);函数
注意:该函数只能接受UTF-8编码数据,若是传递其余格式的数据则会返回null;ui
<?php $arr = array(‘id’=>1,’name’=>’lgx’); echo json_encode($arr);
输出结果为:{"id":1,"name":"lgx"}编码
通讯数据标准格式spa
Codecode |
状态码(200,400等)orm |
Messagexml |
提示信息(邮箱格式不正确;数据返回成功等) |
Data |
返回数据 |
json方式如何封装通讯数据方法
在response.php文件中
<?php class Response{ /** *按照json方式输出通讯数据 *@param integer $code 状态码 *@param string $message 提示信息 *@param array $data 数据 * return string */ public static function json($code,$message='',$data=array()){ $message=iconv('GBK','UTF-8',$message); if(!is_numeric($code)) return ''; $result = array( 'code'=>$code, 'message'=>$message, 'data'=>$data ); echo json_encode($result); } }
在index.php文件中
<?php require_once('./Response.php'); $arr = array('id'=>1,'name'=>'lgx'); Response::json(200,'数据返回成功',$arr);
输出结果为:
{"code":200,"message":"\u6570\u636e\u8fd4\u56de\u6210\u529f","data":{"id":1,"name":"lgx"}}
二、XML方式封装接口数据方法
PHP生成XML数据
组装字符串
<?php public static function xml(){ header("content-type:text/xml");//要设置为text/xml格式 $xml = "<?xml version='1.0' encoding='UTF-8'?>\n"; $xml.="<root>\n"; $xml.="<code>200</code>\n"; $xml.="<message>数据返回成功</message>\n"; $xml.="<data>\n"; $xml.="<id>1</id>\n"; $xml.="<name>lgx</name>\n"; $xml.="</data>\n"; $xml.="</root>"; echo $xml; }
2) 使用系统类
DomDocument
XMLWriter
SimpleXML
xml方式封装接口数据方法
封装方法:
xmlEncode($code,$message=””,$data=array());
data数据分析
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){ //节点不能用数字表示。为了防止数字索引数组中数字$key做为标签,咱们作以下转换 if(is_numeric($key)){ $attr ="id='{$key}'"; $key = 'item'; } $xml.="<$key $attr>"; //若是$value是数组的话,那么就要递归一次 $xml.=is_array($value)?self::xmlToEncode($value):$value; $xml.="</$key>"; } return $xml; }
以下调用:
$data = array( 'id'=>1, 'name'=>'lgx', 'like'=>array("苹果","西瓜","荔枝"), ); Response::xmlEncode(200,"success!",$data);
输出结果为:
三、综合通讯方式封装
封装方法
show($code,$message,$data=array(),$type=’json’); /** *综合方式输出通讯数据 *@param integer $code 状态码 *@param string $message 提示信息 *@param array $data 数据 *@param string $type 数据类型 * return string */ public static function show($code,$message='',$data=array(),$type='json'){ if(!is_numeric($code)){ return ''; } $result=array( 'code'=>$code, 'message'=>$message, 'data'=>$data, ); $type = isset($_GET['format'])?$_GET['format']:$type; if($type == 'json'){ self::json($code,$message,$data); exit; }elseif($type=='array'){ var_dump($result); }elseif($type=='xml'){ self::xmlEncode($code,$message,$data); exit; }else{ //其余 } }
该方法是在前面两种方法的基础上实现判断$type格式来分别调用。
附上完整代码response.php代码
<?php class Response{ /** *综合方式输出通讯数据 *@param integer $code 状态码 *@param string $message 提示信息 *@param array $data 数据 *@param string $type 数据类型 * return string */ public static function show($code,$message='',$data=array(),$type='json'){ if(!is_numeric($code)){ return ''; } $result=array( 'code'=>$code, 'message'=>$message, 'data'=>$data, ); $type = isset($_GET['format'])?$_GET['format']:$type; if($type == 'json'){ self::json($code,$message,$data); exit; }elseif($type=='array'){ var_dump($result); }elseif($type=='xml'){ self::xmlEncode($code,$message,$data); exit; }else{ //其余 } } /** *按照json方式输出通讯数据 *@param integer $code 状态码 *@param string $message 提示信息 *@param array $data 数据 * return string */ public static function json($code,$message='',$data=array()){ $message=iconv('GBK','UTF-8',$message); if(!is_numeric($code)) return ''; $result = array( 'code'=>$code, 'message'=>$message, 'data'=>$data ); echo json_encode($result); } //生成xml格式文件的方法 public static function xml(){ header("content-type:text/xml"); $xml = "<?xml version='1.0' encoding='UTF-8'?>\n"; $xml.="<root>\n"; $xml.="<code>200</code>\n"; $xml.="<message>数据返回成功</message>\n"; $xml.="<data>\n"; $xml.="<id>1</id>\n"; $xml.="<name>lgx</name>\n"; $xml.="</data>\n"; $xml.="</root>"; echo $xml; } /** *按照xml方式输出通讯数据 *@param integer $code 状态码 *@param string $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){ //节点不能用数字表示。为了防止数字索引数组中数字$key做为标签,咱们作以下转换 if(is_numeric($key)){ $attr ="id='{$key}'"; $key = 'item'; } $xml.="<$key $attr>"; //若是$value是数组的话,那么就要递归一次 $xml.=is_array($value)?self::xmlToEncode($value):$value; $xml.="</$key>"; } return $xml; } }