Method of encapsulating communication interface data-xml

WBOY
Release: 2016-07-29 09:03:16
Original
955 people have browsed it

php generates XML data

1) Assemble strings

2) Use system classes

- DomDocument

- XMLWriter

- SimpleXML

The first one here Examples of methods:

<?php

class Response
{

    /**按json方式输出通信数据
     * @param integer $code 状态码
     * @param string $message 提示信息
     * @param array $data 数据
     * return string
     */
    public static function json($code, $message = &#39;&#39;, $data = array())
    {
        if(!is_numeric($code)){
            return &#39;&#39;;
        }

        $result = array(
            &#39;code&#39; => $code,
            'message' => $message,
            'date' => $data
        );

        echo json_encode($result);
        exit;
    }

    public static function xml(){
        header("content-type:text/xml");
        $xml = "<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>\n";
        $xml .= "<root>\n";
        $xml .="<code>200</code>\n";
        $xml .="<message>数据返回成功</message>\n";
        $xml .="<data>\n";
        $xml .="<id>1</id>\n";
        $xml .="<name>ceshi</name>\n";
        $xml .="</data>\n";
        $xml .= "</root>\n";

        echo $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=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
        $xml .="<root>\n";
        $xml .= self::xmlToEncode($result);
        echo $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}{$attr}>";
            $xml .= is_array($value)?self::xmlToEncode($value):$value;  //递归,如果value是数组,递归输出节点。
            $xml .= "</{$key}>\n";
        }
        return $xml;
    }
}

$arr = array(
    'id' => 1,
    'name' => 'xxx',
    'type' => array(4,5,6),
    'test' => array(1,25,345=>array(123,'zifuchuan'))
);
//<0>4</0> <item id=&#39;0&#39;>4</item>

//Response::json(200,'数据返回成功',$arr);
//Response::xml();
Response::xmlEncode(200,'success',$arr);
Copy after login

The above introduces the method of encapsulating communication interface data - xml, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!