PHP以json或xml格式返回請求數(shù)據(jù)的方法
無論是網(wǎng)頁還是移動(dòng)端,都需要向服務(wù)器請求數(shù)據(jù),那么作為php服務(wù)端,如何返回標(biāo)準(zhǔn)的數(shù)據(jù)呢?
現(xiàn)在主流的數(shù)據(jù)格式無非就是json和xml,下面我們來看看如何用php來封裝一個(gè)返回這兩種格式數(shù)據(jù)的類
我們先定義一個(gè)響應(yīng)類
class response{ }
1、以json格式返回?cái)?shù)據(jù)
json格式返回?cái)?shù)據(jù)比較簡單,直接將我們后臺獲取到的數(shù)據(jù),以標(biāo)準(zhǔn)json格式返回給請求端即可
//按json格式返回?cái)?shù)據(jù) 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); }
2、以xml格式返回?cái)?shù)據(jù)
這種方式需要遍歷data里面的數(shù)據(jù),如果數(shù)據(jù)里有數(shù)組還要遞歸遍歷。還有一種特殊情況,當(dāng)數(shù)組的下標(biāo)為數(shù)字時(shí),xml格式會(huì)報(bào)錯(cuò),需要將xml中數(shù)字標(biāo)簽替換
//按xml格式返回?cái)?shù)據(jù) 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'?>"; $xml.="<root>"; $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}>"; } return $xml; } }
3、將兩種格式封裝為一個(gè)方法,完整代碼如下:
class response{ public static function show($code,$message,$data=array(),$type='json'){ /** *按綜合方式輸出通信數(shù)據(jù) *@param integer $code 狀態(tài)碼 *@param string $message 提示信息 *@param array $data 數(shù)據(jù) *@param string $type 數(shù)據(jù)類型 *return string */ if(!is_numeric($code)){ return ''; } $result=array( "code"=>$code, "message"=>$message, "data"=>$data ); if($type=='json'){ self::json($code,$message,$data); exit; }elseif($type=='xml'){ self::xmlEncode($code,$message,$data); exit; }else{ //后續(xù)添加其他格式的數(shù)據(jù) } } //按json格式返回?cái)?shù)據(jù) 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); } //按xml格式返回?cái)?shù)據(jù) 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'?>"; $xml.="<root>"; $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}>"; } return $xml; } } $data=array(1,231,123465,array(9,8,'pan')); response::show(200,'success',$data,'json');
這樣我們調(diào)用show方法時(shí),需要傳遞四個(gè)參數(shù),第四個(gè)參數(shù)為想要返回的數(shù)據(jù)格式,默認(rèn)為json格式,效果如下:
我們再調(diào)用一次show方法,以xml格式返回?cái)?shù)據(jù):
response::show(200,'success',$data,'xml');
效果如下:
這樣我們就完成了對這兩種數(shù)據(jù)格式的封裝,可以隨意返回這兩種格式的數(shù)據(jù)了
以上這篇PHP以json或xml格式返回請求數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- php+Ajax處理xml與json格式數(shù)據(jù)的方法示例
- php實(shí)現(xiàn)xml與json之間的相互轉(zhuǎn)換功能實(shí)例
- PHP生成json和xml類型接口數(shù)據(jù)格式
- PHP實(shí)現(xiàn)返回JSON和XML的類分享
- php json與xml序列化/反序列化
- php 備份數(shù)據(jù)庫代碼(生成word,excel,json,xml,sql)
- PHP封裝的XML簡單操作類完整實(shí)例
- PHP數(shù)組生成XML格式數(shù)據(jù)的封裝類實(shí)例
- php封裝json通信接口詳解及實(shí)例
- PHP封裝返回Ajax字符串和JSON數(shù)組的方法
- PHP封裝XML和JSON格式數(shù)據(jù)接口操作示例
相關(guān)文章
完美的2個(gè)php檢測字符串是否是utf-8編碼函數(shù)分享
這篇文章主要介紹了完美的2個(gè)php檢測字符串是否是utf-8編碼函數(shù)分享,一個(gè)比較強(qiáng)大、一個(gè)比較簡潔,需要的朋友可以參考下2014-07-07windows下apache搭建php開發(fā)環(huán)境
本文詳細(xì)介紹了在Windows2003下使用Apache2.2.21/PHP5.3.5/Mysql5.5.19/phpMyAdmin3.4.9搭建php開發(fā)環(huán)境,本文介紹的非常詳細(xì),需要的朋友快來學(xué)習(xí)吧2015-08-08老司機(jī)傳授Ubuntu下Apache+PHP+MySQL環(huán)境搭建攻略
這篇文章主要介紹了Ubuntu下Apache+PHP+MySQL環(huán)境搭建攻略,文中采用的是LoadModule加載模塊的方式將PHP與Apache服務(wù)器程序連接,需要的朋友可以參考下2016-03-03Laravel基礎(chǔ)-關(guān)于引入公共文件的兩種方式
今天小編就為大家分享一篇Laravel基礎(chǔ)-關(guān)于引入公共文件的兩種方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10開源php中文分詞系統(tǒng)SCWS安裝和使用實(shí)例
這篇文章主要介紹了開源php中文分詞系統(tǒng)SCWS安裝和使用實(shí)例,需要的朋友可以參考下2014-04-04淺析ThinkPHP緩存之快速緩存(F方法)和動(dòng)態(tài)緩存(S方法)(日常整理)
thinkPHP的F方法只能用于緩存簡單數(shù)據(jù)類型,不支持有效期和緩存對象。S()緩存方法支持有效期,又稱動(dòng)態(tài)緩存方法。本文是小編日常整理有關(guān)thinkphp緩存方法,對thinkphp緩存方法感興趣的朋友一起學(xué)習(xí)吧2015-10-10