精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

现在主流的数据格式无非就是json和xml,下面我们来看看如何用php来封装一个返回这两种格式数据的类,下面青岛星网跟大家一起分享。

1、以json格式返回数据

json格式返回数据比较简单,直接将我们后台获取到的数据,以标准json格式返回给请求端即可

//按json格式返回数据
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);
}

2、以xml格式返回数据

这种方式需要遍历data里面的数据,如果数据里有数组还要递归遍历。还有一种特殊情况,当数组的下标为数字时,xml格式会报错,需要将xml中数字标签替换

//按xml格式返回数据
    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;
    }
}

3、将两种格式封装为一个方法,一个相应类代码如下

class response{
    public static function show($code,$message,$data=array(),$type='json'){
        /**
        *按综合方式输出通信数据
        *@param integer $code 状态码
        *@param string $message 提示信息
        *@param array $data 数据
        *@param string $type 数据类型
        *return string
        */
        if(!is_numeric($code)){
            return '';
        }
        $result=array(
            "code"=>$code,
            "message"=>$message,
            "data"=>$data
        );
        if($type=='json'){
            self::json($code,$message,$data);
            exit;
        }elseif($type=='xml'){
            self::xmlEncode($code,$message,$data);
            exit;
        }else{
            //后续添加其他格式的数据
        }
    }
    //按json格式返回数据
    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);
    }
    //按xml格式返回数据
    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;
    }
}
$data=array(1,231,123465,array(9,8,'pan'));
response::show(200,'success',$data,'json');

这样我们调用show方法时,需要传递四个参数,第四个参数为想要返回的数据格式,默认为json格式。

这样我们就完成了对这两种数据格式的封装,可以随意返回这两种格式的数据了

Tags:PHP json xml