標籤: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介面(一)