PHP開發APP介面(一)

來源:互聯網
上載者:User

標籤:blog   io   ar   sp   for   on   資料   2014   log   

php以json或者xml 形式返回給app。明白這點就很好說了,就是把資料封裝成json或者xml,返回給APP

定義抽象APP基類:

<?php/** * 定義API抽象類別*/abstract class Api {const JSON = 'Json';const XML = 'Xml';const ARR = 'Array';/*** 定義Factory 方法* param string $type 返回資料類型*/public static function factory($type = self::JSON) {$type = isset($_GET['format']) ? $_GET['format'] : $type;$resultClass = ucwords($type);require_once('./Response/' . $type . '.php');return new $resultClass();}abstract function response($code, $message, $data);}

以xml形式返回給APP:

<?phpclass Xml extends Api {public function response($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>";$xml .= self::xmlToEncode($result);$xml .= "</root>";echo $xml;}public static  function xmlToEncode($result) {$xml = $attr = '';foreach($result as $key => $value) {                    //判斷索引值對,如果是數字索引值不允許if(is_numeric($key)) {$attr = " id='" . $key . "'";$key = "item";}$xml .= "<{$key}{$attr}>";                        //以遞迴形式返回,主要是因為數組在xml中顯示是array,必須顯示出來具體索引值對$xml .= is_array($value) ? self::xmlToEncode($value) : $value;$xml .= "</{$key}>\n";}return $xml;}}

以json格式返回資料:

<?php/** * 按xml方式輸出通訊資料*/class Json extends Api {public function response($code, $message = '', $data = array()) {if(!(is_numeric($code))) {return '';}$result = array('code' => $code,'message' => $message,'data' => $data);echo json_encode($result);exit;}}

也可以採用這種方式組裝返回資料:

<?phpclass Response {const JSON = "json";/*** 按綜合方式輸出通訊資料* @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $data 資料* @param string $type 資料類型* return string*/public static function show($code, $message = '', $data = array(), $type = self::JSON) {if(!is_numeric($code)) {return '';}$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;$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 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 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 static 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;}}


PHP開發APP介面(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.