Home  >  Article  >  Backend Development  >  PHP method to return requested data in json or xml format

PHP method to return requested data in json or xml format

不言
不言Original
2018-05-31 15:22:221671browse

This article mainly introduces the method of PHP returning request data in json or xml format. It has certain reference value. Now I share it with you. Friends in need can refer to it

Whether it is a web page or The mobile terminal needs to request data from the server, so as a PHP server, how to return standard data?

The current mainstream data formats are nothing more than json and xml. Let's take a look at how to use php to encapsulate a class that returns data in these two formats

Let's first define a response Class

class response{
}

1. Return data in json format

##json format Returning data is relatively simple. Just return the data obtained from our background to the requesting end in standard json format

//按json格式返回数据
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 , Return data in xml format

This method requires traversing the data in data, and if there is an array in the data, it must be traversed recursively. There is also a special case. When the subscript of the array is a number, the xml format will report an error. You need to replace the number tags in the xml with

//按xml格式返回数据
 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.="";
  $xml.=self::xmlToEncode($result);
  $xml.="";
  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.="";
  }
  return $xml;
 }
}

3. Encapsulate the two formats into one method. The complete code is as follows:

class response{
 public static function show($code,$message,$data=array(),$type='json'){
  /**
  *按综合方式输出通信数据
  *@param integer $code 状态码
  *@param string $message 提示信息
  *@param array $data 数据
  *@param string $type 数据类型
  *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{
   //后续添加其他格式的数据
  }
 }
 //按json格式返回数据
 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格式返回数据
 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.="";
  $xml.=self::xmlToEncode($result);
  $xml.="";
  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.="";
  }
  return $xml;
 }
}
$data=array(1,231,123465,array(9,8,'pan'));
response::show(200,'success',$data,'json');

In this way, when we call the show method, we need Pass four parameters, the fourth parameter is the data format you want to return, the default is json format, the effect is as follows:

We call the show method again, in xml format Return data:

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

The effect is as follows:

This way we are done In order to encapsulate these two data formats, you can return data in these two formats at will.

Related recommendations:


You may encounter this when php is installed in CGI mode Attacks and solutions

The above is the detailed content of PHP method to return requested data in json or xml format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn