PHP 開發 APP 介面總結 - JSON 結合 XML 方式封裝通訊介面

來源:互聯網
上載者:User

標籤:

要求:

1.在一個類中封裝多種資料通訊方法(JSON,XML),並且只通過一個入口選擇需要的資料通訊格式

2.用戶端開發工程師可以自行選擇資料轉送格式(GET 方式)

response.php

<?phpclass Response{    const JSON = ‘json‘;    //封裝的綜合方法,預設的資料類型為json    public static function show($code,$message = ‘‘,$data,$type = self::JSON){                if(!is_numeric($code)){            return ‘‘;        }        //供測試數組使用        $result = array(            ‘code‘ => $code,            ‘message‘ => $message,            ‘data‘ => $data        );        //通過get參數判斷通訊資料類型        $typelist = array(‘json‘,‘xml‘,‘array‘); // array為測試使用        if(isset($_GET[‘type‘])){            if(in_array(strtolower($_GET[‘type‘]),$typelist)){                $type = strtolower($_GET[‘type‘]);            }else{                $type = self::JSON;            }        }else{            $type = self::JSON;        }        if($type == ‘json‘){            self::json($code,$message = ‘‘,$data);        }else if($type == ‘xml‘){            self::xml($code,$message = ‘‘,$data);        }else if($type == ‘array‘){            var_dump($result);    //僅供測試        }    }    /**    * 按json方式輸出通訊資料    * @param integer $code 狀態代碼    * @param string $message 提示資訊    * @param array $data 資料    * return string    */    //設定靜態方法    public static 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 static function xml($code,$message,$data){        if(!is_numeric($code)){            return ‘‘;        }        $result = array(            ‘code‘ => $code,            ‘message‘ => $message,            ‘data‘ => $data        );        //修改 http 頭資訊        header("Content-Type:text/xml");        //xml頭資訊        $xml = "<?xml version=‘1.0‘ encoding=‘utf-8‘?>";        //根節點開始標籤        $xml .= "<root>";        $xml .= self::xmlToEncode($result);        //根節點結束標籤        $xml .= "</root>";        echo $xml;        exit();    }    //解析$result至xml    public static function xmlToEncode($data){        $xml = $attr = "";        foreach($data as $k=>$v){            //如果$k是數字(data(code,message,data中的data)資料裡面還含有索引數組),要進行如下判斷            if(is_numeric($k)){                $attr = "id=‘{$k}‘";                $k = ‘item ‘;            }            $xml .= "<{$k}{$attr}>";            //如果$v是數組,則遞迴調用該方法            if(is_array($v)){                $xml .= self::xmlToEncode($v);            }else{                $xml .= $v;            }            $xml .= "</{$k}>";        }        return $xml;    }}

test.php

<?phprequire ‘response.php‘;$data = array(    ‘id‘=>1,    ‘name‘=>‘Mary‘,    ‘type‘=>array(1,3,6) );Response::show(200,‘資料返回成功‘,$data);

測試url:

http://127.0.0.17/php/APP/test.php

http://127.0.0.17/php/APP/test.php?type=json

http://127.0.0.17/php/APP/test.php?type=xml

http://127.0.0.17/php/APP/test.php?type=array

http://127.0.0.17/php/APP/test.php?type=XML (返回 xml 資料)

http://127.0.0.17/php/APP/test.php?type=arr (返回 json 資料)

PHP 開發 APP 介面總結 - JSON 結合 XML 方式封裝通訊介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.