PHP 开发 APP 接口 学习笔记与总结 - JSON 结合 XML 方式封装通信接口

要求:

1.在一个类中封装多种数据通信方法(JSON,XML),并且只通过一个入口选择需要的数据通信格式

2.客户端开发工程师可以自行选择数据传输格式(GET 方式)

response.php

复制代码
<?php

class Response{
    const JSON = 'json';
    //封装的综合方法,默认的数据类型为json
    public static function show($code,$message = '',$data,$type = self::JSON){
        
        if(!is_numeric($code)){
            return '';
        }
        //供测试数组使用
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        //通过get参数判断通信数据类型
        $typelist = array('json','xml','array'); // array为测试使用
        if(isset($_GET['type'])){
            if(in_array(strtolower($_GET['type']),$typelist)){
                $type = strtolower($_GET['type']);
            }else{
                $type = self::JSON;
            }
        }else{
            $type = self::JSON;
        }

        if($type == 'json'){
            self::json($code,$message = '',$data);
        }else if($type == 'xml'){
            self::xml($code,$message = '',$data);
        }else if($type == 'array'){
            var_dump($result);    //仅供测试
        }
    }

    /**
    * 按json方式输出通信数据
    * @param integer $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
        );

        echo json_encode($result);
        exit();
    }

    /**
    * 按 xml 方式输出通信数据
    * @param integer $code 状态码
    * @param string $message 提示信息
    * @param array $data 数据
    * return string
    */
    public static function xml($code,$message,$data){

        if(!is_numeric($code)){
            return '';
        }

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

        //修改 http 头信息
        header("Content-Type:text/xml");
        //xml头信息
        $xml = "<?xml version='1.0' encoding='utf-8'?>";
        //根节点开始标签
        $xml .= "<root>";

        $xml .= self::xmlToEncode($result);

        //根节点结束标签
        $xml .= "</root>";

        echo $xml;
        exit();
    }

    //解析$result至xml
    public static function xmlToEncode($data){
        $xml = $attr = "";
        foreach($data as $k=>$v){
            //如果$k是数字(data(code,message,data中的data)数据里面还含有索引数组),要进行如下判断
            if(is_numeric($k)){
                $attr = "id='{$k}'";
                $k = 'item ';
            }

            $xml .= "<{$k}{$attr}>";
            //如果$v是数组,则递归调用该方法
            if(is_array($v)){
                $xml .= self::xmlToEncode($v);
            }else{
                $xml .= $v;
            }
            $xml .= "</{$k}>";
        }

        return $xml;
    }
}
复制代码

test.php

复制代码
<?php
require 'response.php';

$data = array(
    'id'=>1,
    'name'=>'Mary',
    'type'=>array(1,3,6) 
);

Response::show(200,'数据返回成功',$data);
复制代码

测试url:

http://127.0.0.17/php/APP/test.php

http://127.0.0.17/php/APP/test.php?type=json

http://127.0.0.17/php/APP/test.php?type=xml

http://127.0.0.17/php/APP/test.php?type=array

http://127.0.0.17/php/APP/test.php?type=XML (返回 xml 数据)

http://127.0.0.17/php/APP/test.php?type=arr (返回 json 数据)

posted @   nemo20  阅读(908)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!
访客数:AmazingCounters.com
2016/05/17 起统计
点击右上角即可分享
微信分享提示