zoukankan      html  css  js  c++  java
  • 夺命雷公狗---PHP开发APP接口---4(综合通信方式封装)

    综合通信接口数据方式封装

    show($code,$message='',$data=array(),$type='json');

    封装好之后只要传递一个参数json或者xml就可以得到json或者xml数据

    <?php
        class Ren{
            /**
            *按综合方式输出通信数据
            *@param integer $code 验证码
            *@param string $message 提示信息
            *@param array $data 数据
            *return string $type 数据类型
            *return string 数据
            */
            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);
                    exit;
                }else if($type == 'array'){
                    var_dump($result);  //这里的目的主要是为了做一个调试的功能
                }else if($type == 'xml'){
                    self::xmlEcode($code,$message,$data);
                    exit;
                }else{
                    //后续可以继续写
                }
            }
            
            
            public static function json($code,$message='',$data=array()){
                if(!is_numeric($code)){
                    return '';
                }
                
                $result = array(
                    'id'=>$code,
                    'message'=>$message,
                    'data' => $data
                );
                echo json_encode($result);
                exit;
            }
            
            public static function xmlEcode($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 $k=>$v){
                    if(is_numeric($k)){
                        $attr = " id='{$k}'";
                        $k ='item';
                    }
                    $xml .= "<{$k}{$attr}>";//$k相当于数据里面的节点
                    $xml .= is_array($v) ? self::xmltoEncode($v):$v;//$v相当xml节点里面的数据
                    $xml .= "</{$k}>
    ";
                }
                return $xml;
            }
        }

    测试方法:

    <?php
        include('./6.php');
        //测试的json数据
        $arr = array(
            'id'=> 1,
            'name'=>'lisi',
            'age' => 20
        );
        Ren::show('200','数据返回成功',$arr);
        
        /*
        //测试的xml数据
        $data = array(
            'id' => 1,
            'name' => 'lisi',
            'type' => array(1,7,89,array('5','a','d','d'))
        );
        Ren::show(200,'success',$data);*/
  • 相关阅读:
    linux命令---常用组合
    linux---进程相关的命令
    linux命令---系统监控
    linux命令---find
    linux命令---sort
    linux命令---tar
    linux命令---split
    linux命令---awk进阶
    log4net使用方法
    URL编码:不同的操作系统、不同的浏览器、不同的网页字符集,将导致完全不同的编码结果。
  • 原文地址:https://www.cnblogs.com/leigood/p/4955141.html
Copyright © 2011-2022 走看看