PHP开发APP接口全历程(一)
效劳器端 –> 数据库|缓存 –>调取接口 –>客户端
2、APP接口介绍:(PHP开发APP接口)
PHP面向对象的接口:抽象类,interface定义 ==>interface.php
===>1.很标准
APP接口(通讯接口):通过接口得到数据,将数据填充到APP中
—>APP开发人员关注:恳求APP地址(接口地址)+返回数据
APP(通讯)接口定义:
1.接口地址:http://app.com/api.php?format=xml
2.接口文件:app.php处置一些业务逻辑
3.接口数据
(引荐学习:PHP视频教程)
3.客户端APP通讯:
APP是怎样停止通讯的:
C (接口地址:http://app.com/api.php?format=xml/json) S 客户端APP ------------------------------> 效劳器 <----------------------------- 返回数据
4.客户端APP通讯格局不同
1.xml:扩展标志说话(1.用来标志数据,定义数据类型,是一种同意会员对本人的标志说话停止定义的源说话,xml格局统一,跨平台和说话,非常适合数据传输和通讯,早已成为业界公认的标准)
<?xml version="1.0" encoding="UTF-8"?> <item> <title>测试</title> <test id="1"> <description>测试oen</description> <address>深圳</address> </item>
2.json:一种清凉级别的数据交流格局,具有良好的可读和便于快速编写的特性,可在不一样平台证件停止数据交流,JSON采纳兼容性很高的,完全独立于说话文本格局。这种特性使JSON成为抱负的数据交流说话。
XML的可读性要好,JSON的生成数据性 (json_encode(数组)) 传输速度方面要好
5.APP接口做的那些事:
猎取数据:从数据库中或缓存中猎取数据,然后通过接口数据返回客户端
提交数据:通过接口提交数据给效劳器,然后通过效劳器入库处置,或者其他处置
6.JSON方式封装通讯接口
PHP生成json数据:json_encode($arr);
注释:该函数只能接受UTF-8编码的数据,假如传递其他格局的数据该函数会返回null
通讯数据标注格局:
code 状态码(200 400等) message 提醒信息(邮箱格局不准确;数据返回成功等) data 返回响应的数据 —————————- -JSON code : 200 message :”数据返回成功” -data id :1 name : “测试”
实例:
某个server中: public function json($code,$message = '',$data = array()) { if (!is_numeric($code)){ return '错误'; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); echo json_encode($result); exit; }
某个Controller:
public function jsonsAction() { $arr = array( 'id' => 1, 'name' => 'jiang' ); $k = wei()->zhwCategory()->json(200,'成功咯',$arr); return $k; }
阅读器:http://127.0.0.1/admin/zhw-categorys/jsons
{"code":200,"message":"\u6210\u529f\u54af","data":{"id":1,"name":"jiang"}}
7.PHP生成XML数据:
7.1PHP生成XML数据
1.组装字符串
2.使用系统类:DomDocument
XMLWriter
SimpleXML
如用DomDocument:
<?php $dom = new DomDocument('1.0','utf-8'); $element = $dom->createElement('test','This id root element'); $dom->appendChild($element); echo $dom->saveXML(); ?>
====>结果:
<?xml version="1.0" encoding="utf-8"?> <test>This is the root element</test>
使用组装字符串的简便实例性:
public static function xml() { header("Content-Type:text/html"); $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>测试</name>\n"; $xml .="</data>\n"; $xml .="<root>"; echo $xml; }
7.2封装XML数据办法: 封装办法:xmlEncode($code,$message='',$data = array()); data数据剖析: 1.array('index' => 'api'); 2.array(1,7.89); 详细: server模块下:
public 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'?>\n"; $xml .= "<root>\n"; $xml .=self::xmlToEncode($result); $xml .="</root>"; echo $xml; }
//对数据再处置
public function xmlToEncode($data){ $xml = $attr =""; foreach ($data as $key=>$value){ if(is_numeric($key)){ $attr = "id='{$key}'"; $key = "item"; } $xml .= "<{$key} {$attr}>"; //它们{$key} {$attr}之间要有一个小空格 $xml .=is_array($value) ? self::xmlToEncode($value):$value; $xml .="</{$key}>\n"; } return $xml; }
某个Controller:
public function xmlsAction() { $arr = array( 'id' => 1, 'name' => 'jiang', 'type' =>array(4,5,6), 'test' =>array(1,45,67=>array(1,2,3)), ); $k = wei()->zhwCategory()->xmlEncode(200,'成功咯',$arr); return $k; }
8.综合方式封装通讯数据办法:
封装办法:show($code,$message,$data=array(),$type='json/xml')
终究页面:
server:
<?php namespace Miaoxing\Zhw\Service; use miaoxing\plugin\BaseModel; class ZhwCategory extends BaseModel { const JSON = "json"; /** * 按x综合方式输出通讯数据 * @param integer $code 状态码 * @param string $message 提醒信息 * @param array $data 数据 * @param string $type 数据类型 * return string */ public function show($code,$message='',$data=array(),$type = self::JSON) { if (!is_numeric($code)){ return "错误编码"; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data, ); 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{ //TODO } } /** * 按json方式输出通讯数据 * @param integer $code 状态码 * @param string $message 提醒信息 * @param array $data 数据 * return string */ public function json($code,$message = '',$data = array()) { if (!is_numeric($code)){ return '错误'; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); echo json_encode($result); exit; } /** * 按xml方式输出通讯数据 * @param integer $code 状态码 * @param string $message 提醒信息 * @param array $data 数据 * return string */ public 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'?>\n"; $xml .= "<root>\n"; $xml .=self::xmlToEncode($result); $xml .="</root>"; echo $xml; } //对数据再处置 public 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; $xml .="</{$key}>\n"; } return $xml; } }
Controller: public function jsonsAction() { $arr = array( 'id' => 1, 'name' => 'jiang' ); $k = wei()- >zhwCategory()->json(200,'成功咯',$arr); return $k; } public function xmlsAction() { $arr = array( 'id' => 1, 'name' => 'jiang', 'type' =>array(4,5,6), 'test' =>array(1,45,67=>array(1,2,3)), ); $k = wei()->zhwCategory()->xmlEncode(200,'成功咯',$arr); return $k; } public function showAction() { $arr = array( 'id' => 1, 'name' => 'jiang', 'type' =>array(4,5,6), 'test' =>array (1,45,67=>array(1,2,3)), ); $k = wei()->zhwCategory()->show(200,'成功咯',$arr,'json'); return $k; }
以上就是PHP开发APP接口全历程(一)的具体内容,更多请关注百分百源码网其它相关文章!