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; } }

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念