app數據封裝
<?php
class Makejson{
?? ?/*
?? ?*按json方式生成通信數據數據
?? ?@param integer $code 狀態嗎
?? ?@param string $message 提示信息
?? ?@param array $data 數據
?? ?return string
?? ?//常量JSON表示默認數據類型
?? ?*/
?? ?const JSON='json' ;
? public static function resulut($code,$message='',$data=array(),$type=self::JSON){
?? ??? if(!is_numeric($code)) return '';
?? ?? ?
?? ??? $type=isset($_GET['datatype'])?$_GET['datatype']:self::JSON;
?? ??? $result=array(
?? ???? 'code'=>$code,
?? ??? ?'message'=>$message,
?? ??? ?'data'=>$data
?? ??? );
?? ?? ?
?? ??? if($type=='json'){
?? ??? ??????? self::json($code,$message,$data);
?? ??? ??????? exit;? ?
?? ??? ?}else if($type=='array'){
?? ??? ??? ?? var_dump($result);
?? ??? ??? ?? exit;
?? ??? ?}else if($type=='xml'){
?? ??? ??????? self::xml($code,$message='',$data);
?? ??? ??? ??? exit;
?? ??? ?}else {
?? ??? ????? //? 待添加功能
?? ??? ??? ??? exit;
?? ??? ?}
?? ? ?
?? ? ?
?? ?? }
?? ?
? public static function json($code,$message='',$data=array()){
?? ? ?
?? ?if(!is_numeric($code)) return '';
?? ? $arr=array(
?? ? 'id'=>$code,
?? ? 'message'=>$message,
?? ? 'data'=>$data
?? ?? );
?? ? echo? json_encode($arr);
? }
?
?? /*
?? ?*按xml方式生成通信數據數據
?? ?@param integer $code 狀態嗎
?? ?@param string $message 提示信息
?? ?@param array $data 數據
?? ?return string
?? ?
?? ?*/
?public static function xml($code,$message='',$data=array()){
?? ? if(!is_numeric($code)) return '';
?? ? $arr=array(
?? ? 'id'=>$code,
?? ? 'message'=>$message,
?? ? 'data'=>$data
?? ?? );
?? ? header("Content-Type:text/xml");
?? ? $xml="<?xml version='1.0' encoding='UTF-8' ?> ";
?? ? $xml.="<root>";
?? ? $xml.="<code>".$code."</code>";
?? ? $xml.="<message>".$message."</message>";
?? ? $xml.="<data>";
?? ? $xml.=self::Toxml($data);
?? ? $xml.="</data>";
?? ? $xml.="</root>";
?? ? echo $xml;
? }
? public static function Toxml($arr=array()){
?? ?? $xml=$attr="";
?? ?? foreach($arr as $key=>$val){
?? ?????? if(is_numeric($key)){
?? ??? ??? ?? $attr="id='{$key}'";
?? ??? ??? ?? $key="item";
?? ??? ??? ?? }
?? ???????? $xml.="<{$key} {$attr}>";
?? ??? ??? ?$xml.=is_array($val)?self::Toxml($val):$val;
?? ??? ??? ?$xml.="</{$key}>";
?? ??? }
?? ??? return $xml;
?? ? ?
?? }
}