一、APP接口简介
1、 APP接口介绍
l PHP面向对象中的接口:就是用interface定义的抽象类
如:
<?php
interface video {
public function getVideos();
public function getCount();
}
class Movie implements video{
public function getVideos(){
echo 1;
}
public function getCount(){
echo 2;
}
}
Movie::getVideos();
作用:定义一个接口,提供一个标准。大家都要去准守。
l APP(通信)接口定义:
接口地址:如http://app.com/api.php?format=xml
接口文件:api.php处理一些业务逻辑
接口数据
2、 APP如何进行通信
3、 客户端APP通信数据格式区别
XML扩展标记语言。其中的节点标签可以自定义。XML格式统一,跨平台和语言。
JSON一种轻量级的数据交换格式
l 可读性方面 xml胜出
l 生成数据方面 JSON胜出
l 传输数据方面 JSON胜出
4、 APP接口做的哪些事儿
获取数据 | 从数据库或缓存中获取数据,然后通过接口数据返回给客户端 |
提交数据 | 通过接口提交(get/post)数据给服务器,然后服务器入库处理,或者其他处理 |
封装通信接口方法
1、JSON方式封装接口数据方法
- PHP生成JSON数据
方法:json_encode($value);
注意:该函数只能接受UTF-8编码数据,如果传递其他格式的数据则会返回null;
<?php $arr = array(‘id’=>1,’name’=>’lgx’); echo json_encode($arr);
输出结果为:{“id”:1,”name”:”lgx”}
- 通信数据标准格式
Code | 状态码(200,400等) |
Message | 提示信息(邮箱格式不正确;数据返回成功等) |
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"}}
2、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);
输出结果为:
3、综合通信方式封装
- 封装方法
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; } }
评论