<?php
/*面向对象封装接口
?>
interface video{
public function boom(){}
}
class movie implements video{
public function boom(){
echo "hello word";
}
}
*/
/*APP接口定义
1 接口地址
2 接口文件
3 接口数据
/*面向对象封装接口
?>
interface video{
public function boom(){}
}
class movie implements video{
public function boom(){
echo "hello word";
}
}
*/
/*APP接口定义
1 接口地址
2 接口文件
3 接口数据
json_encode
获取数据
提交数据
*/
//json封装接口
/*
$arr = array(
'id' => 1,
'name'=>'zdd'
);
echo json_encode($arr);
*/
//补充 iconv 转化格式 json_encode函数只能转化UTF-8格式
/*
通信数据必须要有标准格式
提交数据
*/
//json封装接口
/*
$arr = array(
'id' => 1,
'name'=>'zdd'
);
echo json_encode($arr);
*/
//补充 iconv 转化格式 json_encode函数只能转化UTF-8格式
/*
通信数据必须要有标准格式
至少有一下数据
code 状态码 如404
message 提示信息(错误 成功)
data 数据
*/
// php如何封装综合数据方法
// php如何封装Json数据方法
class Response{
const JSON = 'json';
public static function show($code,$message='',$data =array(),$type = self::JSON){
if(!is_numeric($code)){
return '';
}
$type = isset($_GET['format'])?$_GET['format']:self::JSON;
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
);
if($type = 'json'){
self::json($code,$message='',$data);
exit;
}elseif($type = 'xml'){
self::xml($code,$message='',$data);
exit;
}elseif($type = 'array'){
var_dump($res);
}
}
public static function json($code,$message='',$data =array()){
if(!is_numeric($code)){
return '';
}
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
);
echo json_encode($result);
}
code 状态码 如404
message 提示信息(错误 成功)
data 数据
*/
// php如何封装综合数据方法
// php如何封装Json数据方法
class Response{
const JSON = 'json';
public static function show($code,$message='',$data =array(),$type = self::JSON){
if(!is_numeric($code)){
return '';
}
$type = isset($_GET['format'])?$_GET['format']:self::JSON;
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
);
if($type = 'json'){
self::json($code,$message='',$data);
exit;
}elseif($type = 'xml'){
self::xml($code,$message='',$data);
exit;
}elseif($type = 'array'){
var_dump($res);
}
}
public static function json($code,$message='',$data =array()){
if(!is_numeric($code)){
return '';
}
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
);
echo json_encode($result);
}
// php如何封装XML数据方法
/*
1 字符串拼接
2 使用系统类
DomDocument
XMLWriter
SimpleXML
*/
/*
public static function xml(){
//header('Content-type:text/xml')修改header头信息
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .="<root>";
$xml .="<code>200</code>";
$xml .="<message>数据OK</message>"
$xml .="</root>";
echo $xml;
}
*/
/*
1 字符串拼接
2 使用系统类
DomDocument
XMLWriter
SimpleXML
*/
/*
public static function xml(){
//header('Content-type:text/xml')修改header头信息
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .="<root>";
$xml .="<code>200</code>";
$xml .="<message>数据OK</message>"
$xml .="</root>";
echo $xml;
}
*/
//XML形式封装数据接口
public static function xmlEncode($code,$message,$data=array()){
if(!is_numeric($code)){
return '';
}
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
)
header('Content-type:text/xml');
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .="<root>";
$xml .="<code>200</code>";
$xml .="<message>数据OK</message>"
$xml .=self::xmlToEncode($data);
$xml .="</root>";
}
public static function xmlToEncode($data){
$attr = '';
$xml = '';
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;
}
}
?>
public static function xmlEncode($code,$message,$data=array()){
if(!is_numeric($code)){
return '';
}
$res = array(
'code' => $code,
'message'=>$message,
'data' =>$data
)
header('Content-type:text/xml');
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .="<root>";
$xml .="<code>200</code>";
$xml .="<message>数据OK</message>"
$xml .=self::xmlToEncode($data);
$xml .="</root>";
}
public static function xmlToEncode($data){
$attr = '';
$xml = '';
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;
}
}
?>