PHP generates json and xml type interface data format

高洛峰
Release: 2023-03-04 06:04:01
Original
985 people have browsed it

php generates interface communication data

/**
 * 生成接口数据格式
 */
class Response{
  /**
   * [show 按综合方式输出数据]
   * @param [int] $code    [状态码]
   * @param [string] $message [提示信息]
   * @param array $data  [数据]
   * @param [string] $type [类型]
   * @return [string]    [返回值]
   */
  public static function show($code, $message, $data = array(),$type = ''){
    if(!is_numeric($code)){
      return '';
    }
    $result = array(
      'code' => $code,
      'message' => $message,
      'data' => $data
    );
    if($type == 'json'){
      return self::json($code, $message, $data);
    }elseif($type == 'xml'){
      return self::xml($code, $message, $data);
    }else{
      //TODO
    }
  }
  /**
   * [json 按json方式输出数据]
   * @param [int] $code    [状态码]
   * @param [string] $message [提示信息]
   * @param [array] $data  [数据]
   * @return [string]     [返回值]
   */
  public static function json($code, $message, $data = array()){
    if(!is_numeric($code)){
      return '';
    }
    $result = array(
      'code' => $code,
      'message' => $message,
      'data' => $data
    );
    $result = json_encode($result);
    return $result;
  }
  
  /**
   * [xml 按xml格式生成数据]
   * @param [int] $code    [状态码]
   * @param [string] $message [提示信息]
   * @param array $data   [数据]
   * @return [string]     [返回值]
   */
  public static function xml($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;?>\n";
    $xml .= "<root>\n";
    $xml .= self::xmlToEncode($data);
    $xml .= "</root>";
    return $xml;
  }
  
  public static function xmlToEncode($data){
    $xml = &#39;&#39;;
    foreach($data as $key => $value){
      if(is_numeric($key)){
        $attr = "id=&#39;{$key}&#39;";
        $key = "item";
      }
      $xml .= "<{$key} {$attr}>\n";
      $xml .= is_array($value) ? self::xmlToEncode($value) : "{$value}\n";
      $xml .= "</{$key}>\n";
    }
    return $xml;
  }
}
//测试
$grade = array("score" => array(70, 95, 70.0, 60, "70"), "name" => array("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "TianQi"));
$response = new Response();
$result = $response :: show(200,&#39;success&#39;,$grade,&#39;json&#39;);
print_r($result);
Copy after login

The above is the entire content of this article, I hope you all like it.

For more articles related to PHP generating json and xml type interface data format, please pay attention to PHP Chinese website!


Related labels:
xml
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!