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

PHP method to return requested data in json or xml format

不言
Release: 2023-03-28 17:42:01
Original
1841 people have browsed it

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{
}
Copy after login

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);
}
Copy after login

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 version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
  $xml.="<root>";
  $xml.=self::xmlToEncode($result);
  $xml.="</root>";
  echo $xml;
 }
 public static function xmlToEncode($data){
  $xml=$attr=&#39;&#39;;
  foreach($data as $key=>$value){
   if(is_numeric($key)){
    $attr="id=&#39;{$key}&#39;";
    $key="item";
   }
   $xml.="<{$key} {$attr}>";
   $xml.=is_array($value)?self::xmlToEncode($value):$value;
   $xml.="</{$key}>";
  }
  return $xml;
 }
}
Copy after login

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 &#39;&#39;;
  }
  $result=array(
   "code"=>$code,
   "message"=>$message,
   "data"=>$data
  );
  header("Content-Type:text/xml");
  $xml="<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
  $xml.="<root>";
  $xml.=self::xmlToEncode($result);
  $xml.="</root>";
  echo $xml;
 }
 public static function xmlToEncode($data){
  $xml=$attr=&#39;&#39;;
  foreach($data as $key=>$value){
   if(is_numeric($key)){
    $attr="id=&#39;{$key}&#39;";
    $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');
Copy after login

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,&#39;success&#39;,$data,&#39;xml&#39;);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template