Php method for returning request data in JSON or XML format

Source: Internet
Author: User
This article mainly introduces the method of PHP to return request data in JSON or XML format, has a certain reference value, now share to everyone, the need for friends can refer to

Whether it is the Web page or the mobile side, need to request data to the server, then as a PHP service, how to return the standard data?

Now that the mainstream data format is nothing more than JSON and XML, let's look at how to use PHP to encapsulate a class that returns both of these format data.

Let's first define a response class

Class response{}

1. Return data in JSON format

Return data in JSON format is simple, directly to the data we acquired in the background, in the standard JSON format to return to the request side

Returns data in JSON format 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 way you need to traverse the data inside the data, and if there are any arrays in it, recursively traverse them. There is also a special case, when the subscript of the array is a number, the XML format will be error, you need to replace the XML digital tags

Returns data as XML format public static function Xmlencode ($code, $message, $data =array ()) {  if (!is_numeric ($code)) {   return ';  }  $result =array (   "code" and "$code" and   "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, the two formats encapsulated as a method, the complete code is as follows:

Class response{public static function show ($code, $message, $data =array (), $type = ' json ') {/** * output communication data in an integrated manner * @param intege R $code Status code * @param string $message message * @param array $data data * @param string $type data type *return String */if (!is_n  Umeric ($code)) {return ';  } $result =array ("Code" and "$code" and "message" = $message, "data" = $data);   if ($type = = ' json ') {Self::json ($code, $message, $data);  Exit   }elseif ($type = = ' xml ') {Self::xmlencode ($code, $message, $data);  Exit }else{///Subsequent add data in other formats}}//returns data in JSON format public static function JSON ($code, $message, $data =array ()) {if (!is_numeric ($code  ) {return ';  } $result =array ("Code" and "$code" and "message" = $message, "data" = $data); echo Json_encode ($result);  }//returns data as XML format public static function Xmlencode ($code, $message, $data =array ()) {if (!is_numeric ($code)) {return ';  } $result =array ("Code" and "$code" and "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 ("Success", $data, ' json ');

So when we call the Show method, we need to 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 to return the data in XML format:

Response::show ($, ' success ', $data, ' xml ');

The effect is as follows:

This allows us to complete the encapsulation of both data formats and to return the data in both formats.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.