欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP以json或xml格式返回請求數(shù)據(jù)的方法

 更新時(shí)間:2018年05月31日 08:48:59   作者:星空下的噯  
今天小編就為大家分享一篇PHP以json或xml格式返回請求數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

無論是網(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論