您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關PHP中怎么以json或xml格式返回請求數(shù)據(jù),文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1、以json格式返回數(shù)據(jù)
json格式返回數(shù)據(jù)比較簡單,直接將我們后臺獲取到的數(shù)據(jù),以標準json格式返回給請求端即可
//按json格式返回數(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格式返回數(shù)據(jù)
這種方式需要遍歷data里面的數(shù)據(jù),如果數(shù)據(jù)里有數(shù)組還要遞歸遍歷。還有一種特殊情況,當數(shù)組的下標為數(shù)字時,xml格式會報錯,需要將xml中數(shù)字標簽替換
//按xml格式返回數(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、將兩種格式封裝為一個方法,完整代碼如下:
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格式返回數(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格式返回數(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');
這樣我們調用show方法時,需要傳遞四個參數(shù),第四個參數(shù)為想要返回的數(shù)據(jù)格式,默認為json格式,效果如下:
我們再調用一次show方法,以xml格式返回數(shù)據(jù):
response::show(200,'success',$data,'xml');
效果如下:
關于PHP中怎么以json或xml格式返回請求數(shù)據(jù)就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。