前端开发必要了解的知识:写一个简单的接口
- 封装一个接口类 responseApi.class.php
<?php
class ResponseApi{
public function jsonK($code,$message,$data){
$result = array(
"code"=>$code,
"message"=>$message,
"data"=>$data
);
return json_encode($result);
}
}
- 特定返回数据的方法 returnData.php
<?php
require "responseApi.class.php";
$code = 200;
$message = "操作成功";
$data = array(
"name"=>"XXX",
"age"=>"18",
"sign"=>array(
"job"=>"好员工",
"say"=>"good good study"
)
)
$responseApi = new Response;
echo $response-> jsonK($code,$message,$data);
- 发布到服务器上,请求returnData.php就可以获取到数据
网友评论