PHP生成json和xml類型接口數(shù)據(jù)格式
php生成接口通信數(shù)據(jù)
/** * 生成接口數(shù)據(jù)格式 */ class Response{ /** * [show 按綜合方式輸出數(shù)據(jù)] * @param [int] $code [狀態(tài)碼] * @param [string] $message [提示信息] * @param array $data [數(shù)據(jù)] * @param [string] $type [類型] * @return [string] [返回值] */ public static function show($code, $message, $data = array(),$type = ''){ if(!is_numeric($code)){ return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); if($type == 'json'){ return self::json($code, $message, $data); }elseif($type == 'xml'){ return self::xml($code, $message, $data); }else{ //TODO } } /** * [json 按json方式輸出數(shù)據(jù)] * @param [int] $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 ); $result = json_encode($result); return $result; } /** * [xml 按xml格式生成數(shù)據(jù)] * @param [int] $code [狀態(tài)碼] * @param [string] $message [提示信息] * @param array $data [數(shù)據(jù)] * @return [string] [返回值] */ public static function xml($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($data); $xml .= "</root>"; return $xml; } public static function xmlToEncode($data){ $xml = ''; foreach($data as $key => $value){ if(is_numeric($key)){ $attr = "id='{$key}'"; $key = "item"; } $xml .= "<{$key} {$attr}>\n"; $xml .= is_array($value) ? self::xmlToEncode($value) : "{$value}\n"; $xml .= "</{$key}>\n"; } return $xml; } } //測(cè)試 $grade = array("score" => array(70, 95, 70.0, 60, "70"), "name" => array("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "TianQi")); $response = new Response(); $result = $response :: show(200,'success',$grade,'json'); print_r($result);
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
純真IP數(shù)據(jù)庫(kù)的應(yīng)用 IP地址轉(zhuǎn)化成十進(jìn)制
由于純真數(shù)據(jù)庫(kù)中的IP數(shù)據(jù)是和普通IP不同的,所以要轉(zhuǎn)化后才能比對(duì)2009-06-06掌握Laravel框架中容器的管理依賴實(shí)現(xiàn)松耦合
這篇文章主要為大家介紹了掌握Laravel框架中容器管理依賴實(shí)現(xiàn)松耦合,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10ThinkPHP6.0如何利用自定義驗(yàn)證規(guī)則規(guī)范的實(shí)現(xiàn)登陸
這篇文章主要介紹了ThinkPHP6.0如何利用自定義驗(yàn)證規(guī)則規(guī)范的實(shí)現(xiàn)登陸,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12WordPress中用于更新偽靜態(tài)規(guī)則的PHP代碼實(shí)例講解
這篇文章主要介紹了WordPress中用于更新偽靜態(tài)規(guī)則的PHP代碼實(shí)例講解,圍繞flush_rewrite_rules()函數(shù)的使用展開來(lái)講,需要的朋友可以參考下2015-12-12PHP抓取網(wǎng)頁(yè)、解析HTML常用的方法總結(jié)
這篇文章主要介紹了PHP抓取網(wǎng)頁(yè)、解析HTML常用的方法總結(jié),本文只是對(duì)可以實(shí)現(xiàn)這兩個(gè)需求的方法作了總結(jié),只介紹方法,不介紹如何實(shí)現(xiàn),需要的朋友可以參考下2015-07-07ThinkPHP表單令牌錯(cuò)誤的相關(guān)解決方法分析
這篇文章主要介紹了ThinkPHP表單令牌錯(cuò)誤的相關(guān)解決方法,分析了thinkPHP出現(xiàn)令牌錯(cuò)誤的相關(guān)處理過程與解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05Laravel5.5新特性之友好報(bào)錯(cuò)以及展示詳解
這篇文章主要給大家介紹了關(guān)于Laravel5.5新特性之友好報(bào)錯(cuò)以及展示的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08