婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP以json或xml格式返回請求數(shù)據(jù)的方法

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

熱門標簽:智能外呼電銷系統(tǒng) 拉薩打電話機器人 寶安400電話辦理 h5 地圖標注 高識別電銷機器人 電銷機器人-快迭智能 哈爾濱400電話辦理到易號網(wǎng) 沈陽人工智能電銷機器人公司 合肥外呼系統(tǒng)app

無論是網(wǎng)頁還是移動端,都需要向服務(wù)器請求數(shù)據(jù),那么作為php服務(wù)端,如何返回標準的數(shù)據(jù)呢?

現(xiàn)在主流的數(shù)據(jù)格式無非就是json和xml,下面我們來看看如何用php來封裝一個返回這兩種格式數(shù)據(jù)的類

我們先定義一個響應(yīng)類

class response{
}

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ù)組還要遞歸遍歷。還有一種特殊情況,當(dāng)數(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');

這樣我們調(diào)用show方法時,需要傳遞四個參數(shù),第四個參數(shù)為想要返回的數(shù)據(jù)格式,默認為json格式,效果如下:

我們再調(diào)用一次show方法,以xml格式返回數(shù)據(jù):

response::show(200,'success',$data,'xml');

效果如下:

這樣我們就完成了對這兩種數(shù)據(jù)格式的封裝,可以隨意返回這兩種格式的數(shù)據(jù)了

以上這篇PHP以json或xml格式返回請求數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php+Ajax處理xml與json格式數(shù)據(jù)的方法示例
  • php實現(xiàn)xml與json之間的相互轉(zhuǎn)換功能實例
  • PHP生成json和xml類型接口數(shù)據(jù)格式
  • PHP實現(xiàn)返回JSON和XML的類分享
  • php json與xml序列化/反序列化
  • php 備份數(shù)據(jù)庫代碼(生成word,excel,json,xml,sql)
  • PHP封裝的XML簡單操作類完整實例
  • PHP數(shù)組生成XML格式數(shù)據(jù)的封裝類實例
  • php封裝json通信接口詳解及實例
  • PHP封裝返回Ajax字符串和JSON數(shù)組的方法
  • PHP封裝XML和JSON格式數(shù)據(jù)接口操作示例

標簽:泰州 成都 梅州 張家口 林芝 威海 巴中 山東

巨人網(wǎng)絡(luò)通訊聲明:本文標題《PHP以json或xml格式返回請求數(shù)據(jù)的方法》,本文關(guān)鍵詞  PHP,以,json,或,xml,格式,返回,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP以json或xml格式返回請求數(shù)據(jù)的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP以json或xml格式返回請求數(shù)據(jù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 渑池县| 丁青县| 台东县| 泾川县| 含山县| 永康市| 罗定市| 桦川县| 马边| 滨海县| 黄大仙区| 平南县| 临汾市| 伊吾县| 洛宁县| 老河口市| 许昌县| 营口市| 青州市| 鄂州市| 平凉市| 色达县| 武乡县| 商水县| 昌吉市| 莫力| 稷山县| 广州市| 梅州市| 闽清县| 渑池县| 乌拉特前旗| 安图县| 高邑县| 清远市| 林周县| 灵寿县| 长子县| 拉萨市| 大洼县| 东港市|