慕课 php 开发APP接口(一,二)

1/APP接口简介

2/封装通信接口方法

#1 json 返回两种形式的数据

"字符"              //字符串
{"键":"值"}       //json 键值对象形式

json 只返回UTF8 形式的数据,如果转化了编码则返回 null

在练习目录下新建response.php 文件

<?php

$arr = array(
    'id' =>1,
    'name'=>'siangwa',
    'title'=>"标题"
);

$data = "phpstorm无法输入中文";
$newdata = iconv('UTF-8','GBK',$data);
echo $newdata;
echo json_encode($newData);
查看,后面的输出为null

#返回的标准格式是 :

code : 状态码(200,400等)

message:提示信息

data :返回数据


#1.封装返回json 数据

<?php


class Response{

    /**
     * @param $code
     * @param $massage
     * @param $data
     */
    public static function json($code,$massage,$data){
        if(!is_numeric($code)){
            return '';
        }

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

        echo json_encode($result);
        exit;
    }
}

新建测试文件 index.php

<?php

require_once('./response.php');

$arr = array(
    'id'=>1,
    'name'=>'singwa'
);

Response::json(200,'success',$arr);

#生成xml 文件

两种形式

1)组装字符串

2)使用系统类型

    DomDocument

    XmlWriter

    SimpleXML


以下是组装字符串形式

#在response.php 的json 函数下面添加

    public static function xml(){
        header('Content-Type:text/xml');
        $xml = "<?xml version='1.0' encoding='utf-8' ?>";
        $xml .= "<root>";
        $xml .= "<code>200</code>";
        $xml .= "<message>success</message>";
        $xml .= "<data>";
        $xml .= "<id></id>";
        $xml .= "<name>singwa</name>";
        $xml .= "</data>";
        $xml .= "</root>";

        echo $xml;
    }

#在class Response 类外 测试

Response::xml();

#封装xml 数据返回

返回的数据数组有下面两种形式

1. array('index'=>'api')

2. array(1,7,89)

而且 xml 返回的节点 key 不能为数字

/**
 * @param $code
 * @param $massage
 * @param $data
 */
public static function xmlEncode($code,$massage,$data){
    if(!is_numeric($code)){
        return '';
    }
    $result = array(
        'code'=>$code,
        'message'=>$massage,
        'data'=>$data,
    );
    header('Content-Type:text/xml');
    $xml = "<?xml version='1.0' encoding='utf-8' ?>";
    $xml .= "<root>";
    $xml .= self::xmlToEncode($result);
    $xml .= "</root>";
    echo $xml;
}
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;
        $xml .= "</{$key}>";
    }
    return $xml;
}

# 综合封装

    const JSON = 'json';

    /**
     * @param $code
     * @param $massage
     * @param $data
     * @param string $type
     */
    public static function show($code,$massage,$data,$type= self::JSON){
        if(!is_numeric($code)){
            return '';
        }

        $type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
        $result = array(
            'code' => $code,
            'massage' =>$massage,
            'data'=>$data,
        );

        if($type == 'json'){
            self::json($code,$massage,$data);
            exit;
        }elseif($type == 'array'){
            var_dump($result);
        }elseif($type == 'xml'){
            self::xmlEncode($code,$massage,$data);
            exit;
        }else{
            // todo
        }
    }

 

本节课类

class Response{

    const JSON = 'json';

    /**
     * @param $code
     * @param $massage
     * @param $data
     * @param string $type
     */
    public static function show($code,$massage,$data,$type= self::JSON){
        if(!is_numeric($code)){
            return '';
        }

        $type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
        $result = array(
            'code' => $code,
            'massage' =>$massage,
            'data'=>$data,
        );

        if($type == 'json'){
            self::json($code,$massage,$data);
            exit;
        }elseif($type == 'array'){
            var_dump($result);
        }elseif($type == 'xml'){
            self::xmlEncode($code,$massage,$data);
            exit;
        }else{
            // todo
        }
    }

    /**
     * @param $code
     * @param $massage
     * @param $data
     */
    public static function json($code,$massage,$data){
        if(!is_numeric($code)){
            return '';
        }

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

        echo json_encode($result);
        exit;
    }

    /**
     * @param $code
     * @param $massage
     * @param $data
     */
    public static function xmlEncode($code,$massage,$data){
        if(!is_numeric($code)){
            return '';
        }

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

        header('Content-Type:text/xml');
        $xml = "<?xml version='1.0' encoding='utf-8' ?>";
        $xml .= "<root>";
        $xml .= self::xmlToEncode($result);
        $xml .= "</root>";

        echo $xml;

    }

    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;
            $xml .= "</{$key}>";
        }
        return $xml;
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值