Home > php教程 > php手册 > php封装json通信接口

php封装json通信接口

WBOY
Release: 2016-06-13 09:18:59
Original
1061 people have browsed it

php封装json通信接口

创建JSON数据详解:


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


echo json_encode($arr);//这个是创建JSON的关键函数
?>


实现结果
{"id":1,"name":"david"}


注意: json_encode($value);这个函数只能接收utf-8编码的数据。其他格式数据传给该函数返回null;


封装通信接口的数据方法
1.通信数据格式标准:
0111 code 状态码(200,400)如:登录成功200,不成功400
message 提示信息(邮件格式不对,200代表登录成功)
data 返回数据
实例:


demo.php


class Response{
/**
*按json方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string 返回值为json
*/
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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template