app接口数据返回形式

前端之家收集整理的这篇文章主要介绍了app接口数据返回形式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码位置:https://code.csdn.net/u013372487/app_api/tree/master

XML方式:

class Xml extends Api {
    public function response($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>";
        $xml .= self::xmlToEncode($result);
        $xml .= "</root>";
        echo $xml;
    }

    public static  function xmlToEncode($result) {
        $xml = $attr = '';
        foreach($result 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}>\n";
        }
        return $xml;
    }
}

JSON方式:

<?PHP class Json extends Api { public function response($code,$data = array()) { if(!(is_numeric($code))) { return ''; } $result = array( 'code' => $code,'data' => $data ); echo json_encode($result); exit; } }
原文链接:https://www.f2er.com/xml/294395.html

猜你在找的XML相关文章