[PHP开发APP接口]④--XML方式封装通信接口

Paste_Image.png

array( 'index' => 'api' )

response.php

<?php

class Response
{
    /**
     * @param $code 状态码
     * @param $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.="</root>";
        echo $xml;
    }

    public static function xmlToEncode($data)
    {
        $xml = "";
        foreach ($data as $key => $value) {
            $xml .= "<{$key}>";
            $xml .=is_array($value)?self::xmlToEncode($value):$value;
            $xml .= "</{$key}>";
        }
        return $xml;
    }
}

$data=array(
    'id'=>1,
    'name'=>'singwa',
);
Response::xmlEncode(200,'success',$data);

?>
Paste_Image.png

array(3,4,5);

修改的代码

$xml = $attr="";
        foreach ($data as $key => $value) {
            if (is_numeric($key)){
                $attr="id='{$key}'";
                $key="item";
            }
            $xml .= "<{$key} {$attr}>";
 'type'=>array(4,5,6),
 'test'=>array(1,45,67=>array(123,'tsysa'),),

response.php

<?php

class Response
{
  
    /**
     * @param $code 状态码
     * @param $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.="</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(
    'id'=>1,
    'name'=>'singwa',
    'type'=>array(4,5,6),
    'test'=>array(1,45,67=>array(123,'tsysa'),),
);
Response::xmlEncode(200,'success',$data);

?>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,744评论 25 709
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,317评论 6 13
  • 青陂 才过五三坎,又逢观音阁。十里水泥山路,悦目蓝天舒。不管葵亭浪险,大胆闲庭信...
    蓝石村村民阅读 271评论 0 0
  • 图文~清修 我想出去走走 我想出去放放风 我想出去撒撒欢儿 我想出去 我想出去,我想出去 天呐!我要疯了,疯了 ...
    艳青阅读 395评论 0 1