restful api接口代码实例

白昼怎懂夜的黑 提交于 2020-01-14 17:50:04

class Request
{
//允许的请求方式
private static $method_type = array(‘get’, ‘post’, ‘put’, ‘patch’, ‘delete’);
//测试数据
private static $test_class = array(
1 => array(‘name’ => ‘托福班’, ‘count’ => 18),
2 => array(‘name’ => ‘雅思班’, ‘count’ => 20),
);

public static function getRequest()
{
//请求方式
method=strtolower(method = strtolower(_SERVER[‘REQUEST_METHOD’]);
if (in_array(method,self::method, self::method_type)) {
//调用请求方式对应的方法
$data_name = method.Data;returnself::method . ‘Data’; return self::data_name($_REQUEST);
}
return false;
}

//GET 获取信息
private static function getData($request_data)
{
classid=(int)class_id = (int)request_data[‘class’];
//GET /class/ID:获取某个指定班的信息
if (KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ return self::test_class[KaTeX parse error: Expected 'EOF', got '}' at position 12: class_id]; }̲ else {//GET /c…test_class;
}
}

//POST /class:新建一个班
private static function postData(KaTeX parse error: Expected '}', got 'EOF' at end of input: …) { if (!empty(request_data[‘name’])) {
$data[‘name’] = $request_data[‘name’];
data[count]=(int)data[‘count’] = (int)request_data[‘count’];
self::$test_class[] = data;returnself::data; return self::test_class;//返回新生成的资源对象
} else {
return false;
}
}

//PUT /class/ID:更新某个指定班的信息(全部信息)
private static function putData($request_data)
{
classid=(int)class_id = (int)request_data[‘class’];
if ($class_id == 0) {
return false;
}
data=array();if(!empty(data = array(); if (!empty(request_data[‘name’]) && isset($request_data[‘count’])) {
$data[‘name’] = $request_data[‘name’];
data[count]=(int)data[‘count’] = (int)request_data[‘count’];
self::testclass[test_class[class_id] = data;returnself::data; return self::test_class;
} else {
return false;
}
}

//PATCH /class/ID:更新某个指定班的信息(部分信息)
private static function patchData($request_data)
{
classid=(int)class_id = (int)request_data[‘class’];
if (classid==0)returnfalse;if(!empty(class_id == 0) { return false; } if (!empty(request_data[‘name’])) {
self::testclass[test_class[class_id][‘name’] = KaTeX parse error: Expected 'EOF', got '}' at position 23: …_data[‘name’]; }̲ if (isset(request_data[‘count’])) {
self::testclass[test_class[class_id][‘count’] = (int)KaTeX parse error: Expected 'EOF', got '}' at position 24: …data[‘count’]; }̲ return self::test_class;
}

//DELETE /class/ID:删除某个班
private static function deleteData($request_data)
{
classid=(int)class_id = (int)request_data[‘class’];
if (classid==0)returnfalse;unset(self::class_id == 0) { return false; } unset(self::test_class[classid]);returnself::class_id]); return self::test_class;
}
}
————————————————
版权声明:本文为CSDN博主「懒猫1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42408833/article/details/80621190

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!