php开发App接口(张高伟)

来源:互联网 发布:云创大数据实验一体机 编辑:程序博客网 时间:2024/06/15 14:34
APP接口简介:

什么是app接口?app接口就是用服务端程序如php写好的脚本,以供app客户端请求而获得数据的一个东西。比如一个视频app的首页,肯定有一些视频列表,那么当你打开这个app时,这个封装在app里的这个首页其实会去请求一个远程php文件如:http://www.example.com/index.php 去获得需要展示在首页的视频列表数据。前段工程师拿到这些数据,就会按照特定的设计,将这些内容展示出来了。

  接口要实现的目的就是这样。一个app内部通常需要访问多个php接口来获得不同的数据。下面具体讲一讲接口实现的流程以及实现接口需要的一些核心的技术。

思路流程:

 

如何通信:

客户端发送http请求 服务器返回数据。

 

封装通信接口方法:

2-1 JSON方式封装通信接口 (14:39)

先上代码:

[php] view plain copy
  1. response.class.php  
  2.   
  3. <?php  
  4.   
  5. /** 
  6.  *description 用于返回指定数据格式的类 
  7.  *@param $code [int] 返回的状态码 
  8.  *@param $message [string] 返回的状态信息 
  9.  *@param $data [array] 需要返回的数据 
  10.  * 
  11.  */  
  12.   
  13. class Response{  
  14.     public function json($code,$message,$data){  
  15.         $result = array(  
  16.                 "code" => $code,  
  17.                 "message" => $message,  
  18.                 "data" => $data  
  19.             );  
  20.         return json_encode($result);  
  21.     }  
  22. }  


response.class.PHP是一个最简单的返回json格式数据的类,在下面的课程中我们会进一步对其进行完善。

下面贴出接口文件代码:

returndata.php

[php] view plain copy
  1. <?php  
  2. require "response.class.php";    //引入返回信息类  
  3.   
  4. //准备返回数据  
  5. $code = 200;  
  6. $message = "信息请求成功";  
  7. $data = array(  
  8.         "name" => "ruanwnewu",  
  9.         "sex"  => "1",  
  10.         "age"  => "28",  
  11.         "exp" => array(  
  12.                 "2012" => "北京瑞泰新",  
  13.                 "2013" => "兄弟连",  
  14.                 "2014" => "木蚂蚁科技"  
  15.             )  
  16.     );  
  17.   
  18. //实例化response类  
  19. $response = new Response;  
  20.   
  21. //返回数据  
  22. echo $response -> json($code,$message,$data);  

请求returndata.php接口返回的数据如下图所示:

这样就完成了一个最基本的,封装好的json数据通信接口。

 

2-2 PHP生成XML数据 (12:02)

生成XML格式数据一般有三种方法:

  • 拼接字符串
  • domDocument
  • simpleXML

在这里我只讲解第一种方法生成XML数据的方法。上代码:

producexml.php

[php] view plain copy
  1. <?php  
  2. header('Content-Type:text/xml');  
  3. $xml = '<?xml version="1.0" encoding="utf-8" ?>';  
  4. $xml .= '<res>';  
  5. $xml .= '<code>404</code>'."\r\n";  
  6. $xml .= '<message>数据返回成功</message>'."\r\n";  
  7. $xml .= '<data>'."\r\n";  
  8. $xml .= '<name>软文无</name>'."\r\n";  
  9. $xml .= '<sex>1</sex>'."\r\n";  
  10. $xml .= '<age>28</age>'."\r\n";  
  11. $xml .= '</data>';  
  12. $xml .= '</res>';  
  13. echo $xml;  
  14.   
  15.    


2-3 XML方式封装通信接口 (17:50)

上代码:

[php] view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  *description 用于返回指定数据格式的类 
  5.  *@param $code [int] 返回的状态码 
  6.  *@param $message [string] 返回的状态信息 
  7.  *@param $data [array] 需要返回的数据 
  8.  */  
  9.   
  10. class Response{  
  11.     public function json($code,$message,$data){  
  12.         $result = array(  
  13.                 "code" => $code,  
  14.                 "message" => $message,  
  15.                 "data" => $data  
  16.             );  
  17.         return json_encode($result);  
  18.     }  
  19.       
  20.     public function xml($code,$message,$data){  
  21.         $result = array(  
  22.                 "code" => $code,  
  23.                 "message" => $message,  
  24.                 "data" => $data  
  25.             );  
  26.         header('Content-Type:text/xml');  
  27.         $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";  
  28.         $xml .= "<root>";  
  29.         $xml .= self::encodeXml($result);  
  30.         $xml .= "</root>";  
  31.         return $xml;  
  32.     }  
  33.       
  34.     /** 
  35.      *将数据解析为XML字符串 
  36.      */  
  37.       
  38.     public static function encodeXml($data){  
  39.         $attr = $xml = "";  
  40.         foreach($data as $key => $value){  
  41.                 if(is_numeric($key)){  
  42.                     $attr = " id='{$key}'";  
  43.                     $key = "item";  
  44.                 }  
  45.                 $xml .= "<{$key}{$attr}>";  
  46.                 $xml .= is_array($value)?self::encodeXml($value):$value;  
  47.                 $xml .= "</$key>";  
  48.         }  
  49.         return $xml;  
  50.     }  
  51.       
  52. }  

同样用returndata.php调用该接口:

[php] view plain copy
  1. <?php  
  2. require "response.class.php";    //引入返回信息类  
  3.   
  4. //准备返回数据  
  5. $code = 200;  
  6. $message = "信息请求成功";  
  7. $data = array(  
  8.         "name" => "ruanwnewu",  
  9.         "sex"  => "1",  
  10.         "age"  => "28",  
  11.         "exp" => array(  
  12.                 "2012" => "北京瑞泰新",  
  13.                 "2013" => "兄弟连",  
  14.                 "2014" => "木蚂蚁科技"  
  15.             )  
  16.     );  
  17.   
  18. //实例化response类  
  19. $response = new Response;  
  20.   
  21. //返回数据  
  22. echo $response -> xml($code,$message,$data);  

得到如下图所示的XML文件结果

 

2-4 综合方式封装通信数据方法 (11:15)

将两种封装方法综合起来,并能够根据请求的参数来返回指定格式的数据,上代码:

完善后的response类

[php] view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  *description 用于返回指定数据格式的类 
  5.  *@param $code [int] 返回的状态码 
  6.  *@param $message [string] 返回的状态信息 
  7.  *@param $data [array] 需要返回的数据 
  8.  */  
  9.   
  10. class Response{  
  11.     public function show($code,$message,$data,$type="json"){  
  12.         if($type == "json"){  
  13.             $this->json($code,$message,$data);  
  14.         }else{  
  15.             $this->xml($code,$message,$data);  
  16.         }  
  17.     }  
  18.     public function json($code,$message,$data){  
  19.         $result = array(  
  20.                 "code" => $code,  
  21.                 "message" => $message,  
  22.                 "data" => $data  
  23.             );  
  24.         return json_encode($result);  
  25.     }  
  26.       
  27.     public function xml($code,$message,$data){  
  28.         $result = array(  
  29.                 "code" => $code,  
  30.                 "message" => $message,  
  31.                 "data" => $data  
  32.             );  
  33.         header('Content-Type:text/xml');  
  34.         $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";  
  35.         $xml .= "<root>";  
  36.         $xml .= self::encodeXml($result);  
  37.         $xml .= "</root>";  
  38.         return $xml;  
  39.     }  
  40.       
  41.     /** 
  42.      *将数据解析为XML字符串 
  43.      */  
  44.       
  45.     public static function encodeXml($data){  
  46.         $attr = $xml = "";  
  47.         foreach($data as $key => $value){  
  48.                 if(is_numeric($key)){  
  49.                     $attr = " id='{$key}'";  
  50.                     $key = "item";  
  51.                 }  
  52.                 $xml .= "<{$key}{$attr}>";  
  53.                 $xml .= is_array($value)?self::encodeXml($value):$value;  
  54.                 $xml .= "</$key>";  
  55.         }  
  56.         return $xml;  
  57.     }  
  58.       
  59. }  
总结:

1、对于数据的输出最好用json,json具有相当强大的跨平台性,市场上各大主流编程语言都支持json解析,json正在逐步取代xml,成为网络数据的通用格式
2、接口安全,一定要增加接口验证。例如,客户端和服务端针对不同接口统一做好加密方式,服务端在对于每次接口需要都要进行验证。以保证防止接口被恶意刷新或黑客恶意调用,尤其是大型商业应用。
3、对于线上的 API 必须保证所有接口正常且关闭所有的错误信息 => error_reporting(0),在输出JSON 时,不能有任何其它输出,否则,客户端将解析数据失败,直接 Crash!
4、开发 API 和 WEB 有一定的区别,如果是 WEB 的话,可能代码出错了,不会导致特别严重的错误,也许只是导致数据写入和查询失败,也许导致 WEB 的某个部分错位或乱码。但如果是 API,直接 Crash!
原创粉丝点击