zoukankan      html  css  js  c++  java
  • 封装通信接口的方法

    1. JSON方式封装

    • 通过php文件生成JSON数据,方法:json_encode($value)

    注意:该函数只能接收UTF-8编码的数据,如果传递其他类型的数据则会返回null。

    <? php
       $arr = array('id' => 1, 'name' => 'singwa');
       echo json_encode($arr);
     >
    php字符串编码转换函数:iconv();  iconv('UTF-8','GBK',$data); 第一个参数为当前字符串格式,第二个参数为目标格式,第三个参数为待转换数据
    • 通信数据的标准格式:
      • code 状态码(用来表示服务端状态)
      • message 提示信息
      • data 返回数据
    • 用JSON方式进行通信封装

     class Response{        
           /*
             * 按json方式输出通信数据
             * @param integer $code 状态码
             * @param String $message 提示信息
             * @param array $data 数据
             * return String
             */
            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);
                exit;
                 }
              }
    • 调用已经写好的php文件(respons.php)中的方法

    <?php
         require_once(./reponse.php);
         $arr = array(
                   'id' =>1,
                   'name' =>'singwa'
              );
    Response::json(200, 'success', $arr);

    2. XML方式封装
    • 用php生成XML数据
     public static function xml(){
                $xml = "<?xml version = '1.0' encoding = 'UTF-8'?> ";
                $xml .="<root> ";
                $xml .="<code>200</code> ";
                $xml .="<message>数据返回成功</message> ";
                $xml .="<data> ";
                $xml .="<id>1</id> ";
                $xml .="<name>singwa</name> ";
                $xml .="</data> ";
                $xml .="</root> ";
            }
    • 用XML方式进行通信封装 
           /*
             * 按xml方式输出通信数据
             * @param integer $code 状态码
             * @param String $message 提示信息
             * @param array $data 数据
             * return String
             */
            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数据
               
                $xml .="</root>";
            }
           
            public static function xmlToEncode($data){
                $xml = $attr = "";
                foreach ($data as $key => $value){
                    if(is_numeric($key)){
                        $attr = "id = '{$key}'";
                        $key = "item";
                    }
                    $xml .="<{$key}>";
                    $xml .= is_array($value) ? self::xmlToEncode($value):$value; // 判断$value的值是不是数组,是数据就进行递归处理
                    $xml .="</{$key}> ";
                }
                return $xml;
            }
        }
    • 调用已经写好的php文件(respons.php)中的方法 
    <?php
         require_once(./reponse.php);
         $arr = array(
                   'id' =>1,
                   'name' =>'singwa'
              );
    Response::xmlEncode(200, 'success', $arr);

    3. 综合方式封装

    方法:show($code, $type, $message = '', $data = array());

           /*
             * 综合方式输出通信数据
             * @param integer $code 状态码
             * @param String $message 提示信息
             * @param array $data 数据
             *  @param String $type 数据类型
             * return String
             */
            public static function show($code, $type, $message = '', $data = array()){
            if(!is_numeric($code)){
                return '';
            }
                $result = array(
                    'code' => $code,
                    'message' => $message,
                    'data' => $data,
                    );
                   
               
                if($type == 'json'){
                    self::json($code, $message, $data);
                    exit;
                } else if($type == 'array'){
                    var_dump($result);
                }elseif ($type == 'xml') {
                    self::xmlEncode($code, $message, $data);
                    exit;
                }  else {
                    //TODO
                }
        }
  • 相关阅读:
    错误需要理由吗?(SQL Server 不存在或访问被拒绝)
    关于权限设计的轻量级实现
    ppc通过数据线与共享电脑上网
    解读.Net中的命名空间和程序集
    .Net XML 树
    软件开发教父与国内高手论道实录全文
    企业信息化, 该怎么规划?
    ASP.NET Portal Starter Kit中的角色验证
    用js实现类似分享到显示效果
    用js实现同一个页面多个渐变效果
  • 原文地址:https://www.cnblogs.com/siguoyi/p/4445643.html
Copyright © 2011-2022 走看看