/* xml封装接口数据方法 */
// 1. PHP生成xml数据
//$dom = DOMDocument('1.0','utf-8');
//$element = $dom->createElement();
//2. 组装字符串 拼接字符串
Class Response{
/* 按照json方式输出通信数据
*@param $code 状态码
*@param $message 提示信息
*@param array $data 数据
*@return string
* */
public static function json($code,$message = '',$data = array())
{
if(!is_numberic($code)){
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
return json_encode($result);
exit;
}
public static function xml()
{
$xml = "<?xml version='1.0' encoding='utf-8'?>\n";
$xml .= "<root>\n";
$xml .="<code>200</code>\n";
$xml .= "<message>数据返回成功</message>\n";
$xml .= "<data>\n";
$xml .= "<id>1</id>\n";
$xml .= "<name>shibin</name>\n";
$xml .="</data>\n";
$xml .= "</root>\n";
return $xml;
}
}
/* xml封装接口数据方法 */
// 1. PHP生成xml数据
//$dom = DOMDocument('1.0','utf-8');
//$element = $dom->createElement();
//2. 组装字符串 拼接字符串
Class Response{
/* 按照json方式输出通信数据
*@param $code 状态码
*@param $message 提示信息
*@param array $data 数据
*@return string
* */
public static function json($code,$message = '',$data = array())
{
if(!is_numberic($code)){
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
return json_encode($result);
exit;
}
public static function xml()
{
$xml = "<?xml version='1.0' encoding='utf-8'?>\n";
$xml .= "<root>\n";
$xml .="<code>200</code>\n";
$xml .= "<message>数据返回成功</message>\n";
$xml .= "<data>\n";
$xml .= "<id>1</id>\n";
$xml .= "<name>shibin</name>\n";
$xml .="</data>\n";
$xml .= "</root>\n";
return $xml;
}
public static function xmlToEncode($data){
$xml = $attr = '';
foreach($data as $key=>$value){
if(is_numberic($key)){
$attr = "id = '{$key}'";
$key = $item;
}
$xml . "<{$key}{$attr}>\n";
$xml .= is_array($value)?self::xmlToEncode($value):$value; //$value若是数组,递归调用该函数,否则输出即可
$xml .="</{$key}>\n";
}
return $xml;
}
/*综合通信方式方法*/
const JSON = 'json';
public static function show($code,$message = '',$data = array(),$type = self::JSON){
if(!is_numberic($code)){
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] :self::JSON;
$result = array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
if($type == 'json'){
self::json($code,$message,$data);
exit;
}elseif($type == 'array'){
return $result;
}elseif($type == 'xml'){
self::XmlEncode($code,$message,$data);
}else{
//后续的HTML,等数据
}
}
}
$data = array(
'id' => 1,
'name' => 'shibin',
'type'=> array(4,5,6),// key值0,1,2,作为标签了, <0>4</0> -> <item id='0'>4</0>
'test'=> array(1,45,67=>array(123,'test'),)
);
Response::XmlEncode(200,'success',$data);
json/xml方式接口数据封装
原创
©著作权归作者所有:来自51CTO博客作者大宝天天用的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:php使用百度API实现人脸识别
下一篇:Redis实现购物车增删改与清空

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java-封装生成JSON数据和XML数据类
1.背景 借鉴与php中 app接口的实现(php写app接口生成xml和json数据),封装了java版的json
xml json java 生成josn和xml 封装 -
xml方式封装数据方法
1.xml方式封装数据方法 2.demo 运行结果:
xml 数据 封装 数组 html -
grep 参数
一、grep 参数使用Gun grep 选项-b 在搜索到的行的前面打印该行所在的块号码。-c 只显示有多少行匹配 ,而不具体显示匹配的行-h 不显示文件名-i 在字符串比较的时候忽略大小写-l 只显示包含匹配模板的行的文件名清单,
grep 参数 linux grep 参数 linux 正则表达式 字符串