[PHP开发APP接口]⑤--综合方式封装通信数据

Paste_Image.png

修改代码

const JSON="json";
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'){
          self::json($code,$message,$data);
          ecit;
      }else if($type=='array'){
          var_dump($result);
      }else if ($type=='xml'){
          self::xmlEncode($code,$message,$data);
      }else{
          //TODO
      }
  }

response.php

<?php

class Response
{
    const JSON="json";

    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'){
            self::json($code,$message,$data);
            ecit;
        }else if($type=='array'){
            var_dump($result);
        }else if ($type=='xml'){
            self::xmlEncode($code,$message,$data);
        }else{
            //TODO
        }
    }


    public static function json($code, $message = '', $data = array())
    {
        if(!is_numeric($code)){
            return "";
        }else{
            $result=array(
                'code'=>$code,
                'message'=>$message,
                'data'=>$data
            );

            echo json_encode($result);
            exit;
        }
    }

    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;
    }

}


?>

test.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/7/31
 * Time: 13:23
 */
require_once "response.php";
$data=array(
    'id'=>1,
    'name'=>'singwa',
//    'type'=>array(4,5,6),
//    'test'=>array(1,45,67=>array(123,'tsysa'),),
);

//Response::show(200,'success',$data,'json');
/*
 *
{"code":200,"message":"success","data":{"id":1,"name":"singwa","type":[4,5,6],"test":{"0":1,"1":45,"67":[123,"tsysa"]}}}
 */

Response::show(200,'success',$data,'xml');
/*
 * <root>
<code>200</code>
<message>success</message>
<data>
<id>1</id>
<name>singwa</name>
</data>
</root>
 */

Paste_Image.png

修改代码

public static function show($code,$message='',$data=array(),$type=self::JSON){
   $type=isset($_GET['format'])?$_GET['format']:self::JSON;

response.php

<?php

class Response
{
    const JSON="json";

    public static function show($code,$message='',$data=array(),$type=self::JSON){
        if(!is_numeric($code)){
            return '';
        }

        $type=isset($_GET['format'])?$_GET['format']:self::JSON;

        $result=array(
            'code'=>$code,
            'message'=>$message,
            '$data'=>$data
        );

        if($type=='json'){
            self::json($code,$message,$data);
            ecit;
        }else if($type=='array'){
            var_dump($result);
        }else if ($type=='xml'){
            self::xmlEncode($code,$message,$data);
        }else{
            //TODO
        }
    }


    public static function json($code, $message = '', $data = array())
    {
        if(!is_numeric($code)){
            return "";
        }else{
            $result=array(
                'code'=>$code,
                'message'=>$message,
                'data'=>$data
            );

            echo json_encode($result);
            exit;
        }
    }

    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;
    }
}
?>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容