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);*/
  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/leigood/p/4955141.html
Copyright © 2011-2022 走看看