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