PHP通信接口的3种方式

来源:互联网 发布:淘宝店铺卖家入口 编辑:程序博客网 时间:2024/06/13 01:39

1.通信接口有两种数据结构xml和json,他们的区别:

 XMLJSON1.可读性强弱2.生成数据速度弱强3.传输速度弱强2.下面是封装好的3种方法api.php

<?php
/*
 * 三种方式封装app(通信)接口
 * 1.json方式封装通信接口
 * 2.xml方式封装通信接口
 * 3.json-xml综合方式封装通信接口
 */
class Response{
    const JSON ='json';
    
    /* json方式
     * @param $code 状态码
     * @param $message 提示信息
     * @param $data 数据
     */
    public static function json($code,$message='',$data=array()){
        if (!is_numeric($code)){//判断状态码,不为数字时返回空
            return '';
        }
        //将$code,$message,$data组装成新的数组
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        
        echo json_encode($result);
        exit;
    }
    
    
    /*  xml方式
     * @param $code 状态码
     * @param $message 提示信息
     * @param $data 数据
     */
    public static function xml($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'?>\n";
        $xml.= "<root>\n";
        $xml.=self::xmlEncode($result);
        $xml.="</root>";
        
        echo $xml;
    }
    
    public static function xmlEncode($data){
        $xml = $arr = '';
        foreach ($data as $key=>$value){
            if(is_numeric($key)){
                $arr = "id = '$key'";
                $key = "item";
            }
            $xml.="<{$key} {$arr}>\n";
            $xml.=is_array($value)?self::xmlEncode($value):$value;
            $xml.="</{$key}>\n";
        }
        return $xml;
    }
    
    
    /* json-xml综合方式
     * @param $code 状态码
     * @param $message 提示信息
     * @param $data 数据
     * @param $type 通信类型选择json?xml?
     * 
     */
    public static function show($code,$message='',$data=array(),$type){
        if(!is_numeric($code)){
            return '';
        }
        
        $type= isset($_GET['format'])?$_GET['format']:self::JSON;//判断客户端是否有“format”字段,若有,获取;否则赋予默认json方式
        //将三个参数组装成新的数组
        $result = array(
            'code'=>$code,
            'meaasge'=>$message,
            'data'=>$data
        );
        
        if($type=='json'){
            self::json($code,$message,$data);
            exit;
        }elseif ($type=="array"){
            var_dump($result);//仅调试模式,不能用于通信
        }elseif ($type=='xml'){
            self::xml($code,$message,$data);
            exit;
        }else{
            //根据具体业务逻辑做处理
        }
    }
    

下面是测试接口testApi.php

<?php
require_once './api.php';
$data=array(
    'id'=>1,
    'name'=>"php之json",
    'title'=>"通过json封装通信接口",
    //当数组为索引数组时,xml---->节点不能为数字,否则会报错
    'type'=>array(4,5,6),
    //测试当为更复杂的数组时
    'test'=>array(2,6,32=>array(3,"测试"))
);
$result =array(
    'id'=>1,
    'name'=>"php之xml",
    'title'=>"通过xml封装通信接口",
    //当数组为索引数组时,xml---->节点不能为数字,否则会报错
    'type'=>array(4,5,6),
    //测试当为更复杂的数组时
    'test'=>array(2,6,32=>array(3,"测试"))
);
//Response::json(200, '访问数据成功',$data);
//Response::xml(200,'访问数据成功',$result);
Response::show(200, '访问数据成功',$result,'');//$type这个参数可以由客户端决定选用哪种通信接口方式 xml或json当不选择时,默认为json(在api.php中做了处理)

?>

3.总结:根据需求可选择合适的方式

0 0
原创粉丝点击