国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

PHP開發(fā)APP接口之封裝通訊接口

2019-11-08 01:05:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

php開發(fā)APP接口之封裝通訊接口


按json方式輸出通信數(shù)據(jù)


/** * 按json方式輸出通信數(shù)據(jù) * @param integer $code 狀態(tài)碼 * @param string $message 提示信息 * @param array $data 數(shù)據(jù) * 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方式輸出通信數(shù)據(jù)


/** * 按xml方式輸出通信數(shù)據(jù) * @param integer $code 狀態(tài)碼 * @param string $message 提示信息 * @param array $data 數(shù)據(jù) * 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; }

按綜合方式輸出通信數(shù)據(jù)


const JSON = "json"; /** * 按綜合方式輸出通信數(shù)據(jù) * @param integer $code 狀態(tài)碼 * @param string $message 提示信息 * @param array $data 數(shù)據(jù) * @param string $type 數(shù)據(jù)類型 * 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 } }

這種方式可根據(jù)App端傳入的format格式來(lái)判斷是返回json還是xml。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青阳县| 平湖市| 山阴县| 凤庆县| 邻水| 韶山市| 和龙市| 天峨县| 东兰县| 林口县| 丹棱县| 光泽县| 武乡县| 页游| 文成县| 扶绥县| 翁源县| 远安县| 安平县| 罗江县| 固原市| 苏尼特左旗| 大埔县| 吉隆县| 清原| 玛多县| 改则县| 夏邑县| 长葛市| 商河县| 芜湖县| 静宁县| 什邡市| 广西| 夏津县| 台湾省| 邯郸县| 木兰县| 双鸭山市| 信宜市| 信丰县|