php生成 JSON 数据
方法json_encode($value);
该函数只能接受utf-8编码的数据,如果传递其他格式的数据函数返回null<?php
header("content_type:text/html;charset=utf8");
$arr=array(
'id'=>1,
'name'=>'siangwa'
);
echo json_encode($arr);
2.封装一个返回json简单的方法
response.php<?php
class Response{
/*
*按json方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
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;
}
}
test.php<?php
header('content-type:text/html;charset=utf8');
require_once('./response.php');
$arr=array(
'id'=>1,
'name'=>'singwa'
);
Response::json(200,'数据返回成功',$arr);
结果:
/**********************************************************************************************************************/
3.php生成xml数据
1>字符串组装
2>使用系统类
4.写一个简单的xml数据方法
response.phpclass Response{
public static function xml()
{
header("content-type:text/xml;charset=utf8");
$xml="<?xml version='1.0' encoding='UTF-8'?>\n";
$xml.="\n";
$xml.="200
\n";
$xml.="数据返回成功\n";
$xml.="\n";
$xml.="1\n";
$xml.="singwa\n";
$xml.="\n";
$xml.="";
echo $xml;
}
}
Response::xml();
结果:
/**********************************************************************************************************************/
5.xml方式封装接口数据方法
response.phpclass Response{
/*
*按xml方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
public static function xmlIEncode($code,$message,$data=array())
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
header("content-type:text/xml");
$xml="<?xml version='1.0' encoding='UTF-8'?>\n";
$xml.="\n";
$xml.=self::xmlToEncode($result);
$xml.="";
echo $xml;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr=" id='{$key}'";
$key="item";
}
$xml.="";
$xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="{$key}>\n";
}
return $xml;
}
}
$data=array(
'id'=>1,
'name'=>'towan',
'type'=>array(1,23,4)
);
Response::xmlIEncode(200,'success',$data);
结果:
/**********************************************************************************************************************/
6.封装通信接口数据方法
response.php<?php
header("content-type:text/html;charset=utf8");
class Response{
const JSON="json";
/*
*按综合方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*@param string $type 数据类型
*return string
*/
public static function show($code,$message='',$data=array(),$type=self::JSON)
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
// echo $type;die;
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
if($type == 'json'){
self::json($code,$message,$data);
exit;
}else if($type == 'array'){
var_dump($result);
}else if($type == 'xml'){
self::xmlIEncode($code,$message,$data);
exit;
}else{
//后续
}
}
/*
*按json方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
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;
}
/*
*按xml方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
public static function xmlIEncode($code,$message,$data=array())
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
header("content-type:text/xml");
$xml="<?xml version='1.0' encoding='UTF-8'?>\n";
$xml.="\n";
$xml.=self::xmlToEncode($result);
$xml.="";
echo $xml;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
// $attr="id='{$key}'";
$attr=" id='{$key}'";
$key="item";
}
$xml.="";
$xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="{$key}>\n";
}
return $xml;
}
}
test.php<?php
require_once('./response.php');
$data=array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6),
'test'=>array(1,23,45=>array(213,'asdfa'))
);
Response::show(200,'数据返回成功',$data);
/*************************************************************************************************************************/
代码下载
https://pan.baidu.com/s/16pbWruiKmtczXeikM_lNIg