PHP开发APP接口全过程(一)

来源:互联网 发布:腾鹰软件 编辑:程序博客网 时间:2024/05/16 19:40

1、学习要点:
服务器端 –> 数据库|缓存 –>调用接口 –>客户端
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.接口数据

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:

<?phpnamespace 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;    }
原创粉丝点击