zoukankan      html  css  js  c++  java
  • 夺命雷公狗---PHP开发APP接口---3(XML方式封装接口数据方法)

    封装方法

    xmlEncode($code,$message='',$data=array())

    data数据分析

    1...array('index' => 'api');

    <?php
        /**
        *按xml的形式输出通信接口
        *param integer $code验证码
        *param string $message提示信息
        *param array $data数据
        *return    string
        */
        class Ren{
            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 = '';
                foreach($data as $k=>$v){
                    $xml .= "<{$k}>";//$k相当于数据里面的节点
                    $xml .= is_array($v) ? self::xmltoEncode($v):$v;//$v相当xml节点里面的数据//为了方法里面还是数组所以加上了一个递归调用让他继续调用foreach
                    $xml .= "</{$k}>
    ";
                }
                return $xml;
            
            }
        }
        $data = array(
            'id' => 1,
            'name' => 'lisi',
        );
        Ren::xmlEcode(200,'success',$data);

    2...array(1,7,89)

    如果我传进来的数组是这样的,他就会报错,

        $data = array(
            'id' => 1,
            'name' => 'lisi',
            'type' => array(1,7,89)
        );

    她会认为数组里面的下标也是节点,但是xml里面有一个原则,数字不能为节点。

    我们可以这样来解决问题所在,

    原本是这种形式,<0>1<0>
    可改造成这种形式的<item id="0">1</item>

    代码如下:

    <?php
        /**
        *按xml的形式输出通信接口
        *param integer $code验证码
        *param string $message提示信息
        *param array $data数据
        *return    string
        */
        class Ren{
            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;
            }
        }
        $data = array(
            'id' => 1,
            'name' => 'lisi',
            'type' => array(1,7,89,array('5','a','d','d'))
        );
        Ren::xmlEcode(200,'success',$data);

    这样就可以实现了所谓的大小通杀的效果了

  • 相关阅读:
    整合规则引擎urule
    vue学习
    发送put请求,get请求
    jpa自定义字段
    spring的3种配置方式
    netty
    springsercurity和shiro
    git报错
    Scrapy全站数据爬取
    python操作Excel模块openpyxl
  • 原文地址:https://www.cnblogs.com/leigood/p/4955126.html
Copyright © 2011-2022 走看看