PHP开发APP接口(一)

php以json或者xml 形式返回给app。明白这点就很好说了,就是把数据包装成json或者xml,返回给APP

定义抽象APP基类:

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. /** 
  3.  * 定义API抽象类 
  4. */  
  5. abstract class Api {  
  6.   
  7.     const JSON = 'Json';  
  8.     const XML = 'Xml';  
  9.     const ARR = 'Array';  
  10.       
  11.     /** 
  12.     * 定义工厂方法 
  13.     * param string $type 返回数据类型 
  14.     */  
  15.     public static function factory($type = self::JSON) {  
  16.         $type = isset($_GET['format']) ? $_GET['format'] : $type;  
  17.         $resultClass = ucwords($type);  
  18.         require_once('./Response/' . $type . '.php');  
  19.         return new $resultClass();  
  20.     }  
  21.   
  22.     abstract function response($code, $message, $data);  
  23. }  


以xml形式返回给APP:

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. class Xml extends Api {  
  3.     public function response($code, $message = '', $data = array()) {  
  4.         if(!is_numeric($code)) {  
  5.             return '';  
  6.         }  
  7.   
  8.         $result = array(  
  9.             'code' => $code,  
  10.             'message' => $message,  
  11.             'data' => $data  
  12.         );  
  13.   
  14.         header('Content-Type:text/xml');  
  15.         $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";  
  16.         $xml .= "<root>";  
  17.         $xml .= self::xmlToEncode($result);  
  18.         $xml .= "</root>";  
  19.         echo $xml;  
  20.     }  
  21.   
  22.     public static  function xmlToEncode($result) {  
  23.         $xml = $attr = '';  
  24.         foreach($result as $key => $value) {  
  25.                     //判断键值对,如果是数字键值不允许  
  26.             if(is_numeric($key)) {  
  27.                 $attr = " id='" . $key . "'";  
  28.                 $key = "item";  
  29.             }  
  30.             $xml .= "<{$key}{$attr}>";  
  31.                         //以递归形式返回,主要是因为数组在xml中显示是array,必须显示出来具体键值对  
  32.             $xml .= is_array($value) ? self::xmlToEncode($value) : $value;  
  33.             $xml .= "</{$key}>\n";  
  34.         }  
  35.         return $xml;  
  36.     }  
  37. }  


以json格式返回数据:

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. /** 
  3.  * 按xml方式输出通信数据 
  4. */  
  5. class Json extends Api {  
  6.     public function response($code, $message = '', $data = array()) {  
  7.         if(!(is_numeric($code))) {  
  8.             return '';  
  9.         }  
  10.   
  11.         $result = array(  
  12.             'code' => $code,  
  13.             'message' => $message,  
  14.             'data' => $data  
  15.         );  
  16.   
  17.         echo json_encode($result);  
  18.         exit;  
  19.     }  
  20. }  


也可以采用这种方式组装返回数据:

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
    1. <?php  
    2.   
    3. class Response {  
    4.     const JSON = "json";  
    5.     /** 
    6.     * 按综合方式输出通信数据 
    7.     * @param integer $code 状态码 
    8.     * @param string $message 提示信息 
    9.     * @param array $data 数据 
    10.     * @param string $type 数据类型 
    11.     * return string 
    12.     */  
    13.     public static function show($code, $message = '', $data = array(), $type = self::JSON) {  
    14.         if(!is_numeric($code)) {  
    15.             return '';  
    16.         }  
    17.   
    18.         $type = isset($_GET['format']) ? $_GET['format'] : self::JSON;  
    19.   
    20.         $result = array(  
    21.             'code' => $code,  
    22.             'message' => $message,  
    23.             'data' => $data,  
    24.         );  
    25.   
    26.         if($type == 'json') {  
    27.             self::json($code, $message, $data);  
    28.             exit;  
    29.         } elseif($type == 'array') { //适合调试代码  
    30.             var_dump($result);  
    31.         } elseif($type == 'xml') {  
    32.             self::xmlEncode($code, $message, $data);  
    33.             exit;  
    34.         } else {  
    35.             // TODO  
    36.         }  
    37.     }  
    38.     /** 
    39.     * 按json方式输出通信数据 
    40.     * @param integer $code 状态码 
    41.     * @param string $message 提示信息 
    42.     * @param array $data 数据 
    43.     * return string 
    44.     */  
    45.     public static function json($code, $message = '', $data = array()) {  
    46.           
    47.         if(!is_numeric($code)) {  
    48.             return '';  
    49.         }  
    50.   
    51.         $result = array(  
    52.             'code' => $code,  
    53.             'message' => $message,  
    54.             'data' => $data  
    55.         );  
    56.   
    57.         echo json_encode($result);  
    58.         exit;  
    59.     }  
    60.   
    61.     /** 
    62.     * 按xml方式输出通信数据 
    63.     * @param integer $code 状态码 
    64.     * @param string $message 提示信息 
    65.     * @param array $data 数据 
    66.     * return string 
    67.     */  
    68.     public static function xmlEncode($code, $message, $data = array()) {  
    69.         if(!is_numeric($code)) {  
    70.             return '';  
    71.         }  
    72.   
    73.         $result = array(  
    74.             'code' => $code,  
    75.             'message' => $message,  
    76.             'data' => $data,  
    77.         );  
    78.   
    79.         header("Content-Type:text/xml");  
    80.         $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";  
    81.         $xml .= "<root>\n";  
    82.   
    83.         $xml .= self::xmlToEncode($result);  
    84.   
    85.         $xml .= "</root>";  
    86.         echo $xml;  
    87.     }  
    88.   
    89.     public static function xmlToEncode($data) {  
    90.   
    91.         $xml = $attr = "";  
    92.         foreach($data as $key => $value) {  
    93.             if(is_numeric($key)) {  
    94.                 $attr = " id='{$key}'";  
    95.                 $key = "item";  
    96.             }  
    97.             $xml .= "<{$key}{$attr}>";  
    98.             $xml .= is_array($value) ? self::xmlToEncode($value) : $value;  
    99.             $xml .= "</{$key}>\n";  
    100.         }  
    101.         return $xml;  
    102.     }  
    103.   
    104. }  
posted @ 2016-04-02 12:25  哦先生  阅读(1181)  评论(0编辑  收藏  举报