PHP開發APP接口2——封裝通訊接口方法

第2章   封裝通訊接口方法php

一、JSON方式封裝接口數據方法json

  • PHP生成JSON數據數組

方法:json_encode($value);函數

注意:該函數只能接受UTF-8編碼數據,若是傳遞其餘格式的數據則會返回null;ui

<?php
   $arr = array(‘id’=>1,’name’=>’lgx’);
   echo json_encode($arr);

輸出結果爲:{"id":1,"name":"lgx"}編碼

  • 通訊數據標準格式spa

Codecode

狀態碼(200,400)orm

Messagexml

提示信息(郵箱格式不正確;數據返回成功等)

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"}}

 二、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);

輸出結果爲:

三、綜合通訊方式封裝

  • 封裝方法

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;
           }
          
  }
相關文章
相關標籤/搜索