zoukankan      html  css  js  c++  java
  • php 封装json xml通讯数据方法 php开发APP接口

    //php开发APP接口

    class Response{
    const JSON="json";
    public static function show($code,$message,$data=array(),$type=self::JSON){

    
    

    if(!is_numeric($code)){
    return '';
    }
    $type = isset($_GET['fomat'])? $_GET['fomat'] : self::JSON;

    
    

    $result=array(
    'code'=>$code,
    'message'=>$message,
    'data'=>$data
    );

    
    

    if($type=='json'){
    self::json($code,$message,$data);
    exit;
    }elseif($type=="array"){
    var_dump($result);
    }elseif($type=='xml'){
    self:: xmlEncode($code,$message,$data);
    exit;
    }else{
    echo 'do something';//
    }

    
    

    }

    
    
    
    
    

    public static function json($code,$message,$data=array()){
    if( !is_numeric($code) ){
    return '';
    }
    $result=array(
    'code'=>$code,
    'message'=>$message,
    'data'=>$data
    );

    
    

    echo json_encode($result);
    exit;
    }

    
    


    public static function xmlEncode(){
    header("Content-Type:type/xml");//输出xml格式数据
    $xml="<?xml version='1.0' encoding='UTF-8'?> ";
    $xml.="<root> ";
    $xml.="<code>200</code> ";
    $xml.="<message>数据返回成功</message> ";
    $xml.="<data> ";
    $xml.="<id>1</id> ";
    $xml.="<name>kmong</name> ";
    $xml.="</data> ";
    $xml.="</root> ";
    echo $xml;
    }

    
    

    }

    
    


    //$res=new Response;
    //$data=$res->show(200,'成功',array('name'=>'tim','age'=>12));
    //var_dump($data);

    
    
    
    
    
    Response::json(200,"success",$array);
    
     综合封装 xml  json格式的通讯数据

    $data=array(
    id=>1,
    name=>'Tom',
    'type'=>array(4,5,6),
    'test'=>array(1,34,22=>array(1212,'test'));
    );

    Response::show(200,'success,$data,'array');

    访问方法 添加get判断后
    locahost//xxx.php?fomate=json
  • 相关阅读:
    keil编译器从ac5迁移到ac6
    分享一个简单易用的软件定时器模块(MultiTimer)——基于keil+stm32f103zet+hal库(裸机实现)
    PID动图——很形象
    jlink之j-scope使用
    Keil的RTX特性
    Keil5创建基于RTX的STM32工程(转载+自己的体会)
    CMSIS-RTOS 简介
    Chapter 3 Phenomenon——13
    Chapter 3 Phenomenon——12
    Chapter 3 Phenomenon——11
  • 原文地址:https://www.cnblogs.com/itcx/p/4431874.html
Copyright © 2011-2022 走看看