Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
286 lines (248 loc) · 10.4 KB
/
Copy pathindex.php
File metadata and controls
286 lines (248 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/1/10
* Time: 10:19
* API 接口处理入口文件
*/
//自动加载函数
require_once(__DIR__ . '/autoload.php');
spl_autoload_register(["Autoload", "loadFile"]);
$config = require(__DIR__ . '/params.php');
//全局常量
//require_once(__DIR__ . '/commonParams.php');
CONST METHOD_GET = 'GET';//获取资源-返回要获取的单个或多个资源
CONST METHOD_POST = 'POST';//新建资源-返回新建的资源
CONST METHOD_PUT = 'PUT';//更新资源-返回更新后的资源
CONST METHOD_DELETE = 'DELETE';//删除资源-返回空文档
//返回结果信息及状态码
CONST SUCCESS_MSG_GET = '获取成功';
CONST SUCCESS_CODE_GET = 200;
CONST SUCCESS_MSG_CREATE = '添加成功';
CONST SUCCESS_CODE_CREATE = 201;
CONST SUCCESS_MSG_UPDATE = '更新成功';
CONST SUCCESS_CODE_UPDATE = 201;
CONST SUCCESS_MSG_DELETE = '删除成功';
CONST SUCCESS_CODE_DELETE = 204;
CONST COMMON_ERROR_CODE = 299;
class Restful{
/**
* 请求方法
* @var string
*/
private $_requestMethod;
/**
* 允许的请求方式
* @var array
*/
private $_allowRequestMethod = ['GET', 'POST', 'PUT', 'DELETE'];
/**
* 请求方式对应的操作方法前缀
* @var array
*/
private $methodAction = [
METHOD_GET => 'get',
METHOD_POST => 'create',
METHOD_PUT => 'update',
METHOD_DELETE => 'delete',
];
/**
* 请求的资源名称
* @var string
*/
private $_resourceName;
/**
* 请求的资源操作类型
* @var string
*/
private $_actionName;
/**
* 允许请求的资源列表
* @var array
*/
private $_allowResources = ["article",];
public function __construct(){
$this->setRequestMethod();
}
/**
* 设置当前处理请求的请求方式
*/
private function setRequestMethod(){
$this->_requestMethod = $_SERVER['REQUEST_METHOD'];
if (!in_array($this->_requestMethod, $this->_allowRequestMethod)) {
//请求方式不在允许列表中
throw new Exception('不允许的请求方法', 401);
}
}
public function run(){
try{
//获取请求参数
$reqData = $this->_resolveUrl();
$respData = '';
global $config;
switch($this->_requestMethod){
case METHOD_GET:
/**
* 获取文章
*/
//获取数据库连接句柄
$dbInst = DB::getDBLink($config['db']['host'], $config['db']['username'], $config['db']['userpwd']);
$resInst = new $this->_resourceName($dbInst);
$resMethod = $this->_actionName;
$respData = $resInst->$resMethod($reqData);
break;
case METHOD_POST:
/**
* 添加文章
*/
//获取数据库连接句柄
$dbInst = DB::getDBLink($config['db']['host'], $config['db']['username'], $config['db']['userpwd']);
$resInst = new $this->_resourceName($dbInst);
$resMethod = $this->_actionName;
$respData = $resInst->$resMethod($reqData);
break;
case METHOD_PUT:
/**
* 修改文章
*/
//获取数据库连接句柄
$dbInst = DB::getDBLink($config['db']['host'], $config['db']['username'], $config['db']['userpwd']);
$resInst = new $this->_resourceName($dbInst);
$resMethod = $this->_actionName;
$respData = $resInst->$resMethod($reqData);
break;
case METHOD_DELETE:
/**
* 删除文章
*/
/**
* 暂时无法通过非url部分获取DELETE请求体, 因此将请求参数放于url中
*/
//获取数据库连接句柄
$dbInst = DB::getDBLink($config['db']['host'], $config['db']['username'], $config['db']['userpwd']);
$resInst = new $this->_resourceName($dbInst);
$resMethod = $this->_actionName;
$respData = $resInst->$resMethod($reqData);
break;
}
header("Content-Type", "application/json;charset=UTF-8");
echo $respData;
die;
} catch(Exception $e){
echo "错误代码: " . $e->getCode() . " ";
echo $e->getMessage() ;
die;
}
}
/**
* 根据请求方式解析需要传递的参数
*/
private function _resolveUrl(){
//从 SERVER 信息中判断当前需要请求哪个接口
$serverPar = $_SERVER;
//获取除host外的链接 ** http://learngit.me/api/index.php/articles?name=donghan
$requestURI = trim($serverPar['DOCUMENT_URI']);//host之后不含querystring的 字符串,如"/api/index.php/articles"
// 以 '/' 分隔该链接,得到请求的资源名称及参数
$routeArr = explode('/', $requestURI);
$resourceName = array_pop($routeArr);//需要操作的资源名称,复数形式
if (trim($resourceName) == 'index.php') {
throw new Exception('请指定所请求的接口', COMMON_ERROR_CODE);
} else {
//判断资源名称是否包含 's'
if (false !== stripos($resourceName, 's')) {
//首先移除资源名称右侧的复数 s,然后首字符转为大写
$this->_resourceName = ucFirst(rtrim($resourceName, 's'));
} else {
//将首字符转为大写
$this->_resourceName = ucFirst($resourceName);
}
}
$reqData = [];//请求参数
switch($this->_requestMethod){
case METHOD_GET:
/**
* 查询操作
*/
//设置需要调用的资源操作方法
$this->_actionName = $this->methodAction[METHOD_GET] . $this->_resourceName;
$queryString = trim($_SERVER['QUERY_STRING']);
//需要传递给操作方法的查询参数 array
//查询字符串中的键值对数组
$queryParam = [];
if (!empty($queryString) && count($queryString) > 0) {
//请求链接中包含参数,根据参数查找资源
$queryParam = explode('&', $queryString);
}
//将查询字符串依次放入参数数组中
foreach($queryParam as $v){
list($queryKey, $queryVal) = explode('=', $v);
$reqData[$queryKey] = $queryVal;
}
break;
case METHOD_POST:
/**
* 添加操作
*/
//设置需要调用的资源操作方法
$this->_actionName = $this->methodAction[METHOD_POST] . $this->_resourceName;
//获取参数
//$postData = isset($_POST["data_info"]) ? json_decode($_POST["data_info"], true) : [];
$rawData = json_decode(file_get_contents("php://input"), true);
$reqData = isset($rawData["data_info"]) ? $rawData["data_info"] : [];
if (!preg_match("/[a-zA-z]+/", $resourceName)) {
throw new Exception('接口地址错误', COMMON_ERROR_CODE);
//$respData = json_encode(['code' => COMMON_ERROR_CODE,'msg' => '接口地址错误'], JSON_UNESCAPED_UNICODE);
}
break;
case METHOD_PUT:
/**
* 修改操作
*/
//设置需要调用的资源操作方法
$this->_actionName = $this->methodAction[METHOD_PUT] . $this->_resourceName;
//获取修改 参数
//$postData = isset($_POST["data_info"]) ? json_decode($_POST["data_info"], true) : [];
$rawData = json_decode(file_get_contents("php://input"), true);
$reqData = isset($rawData["data_info"]) ? $rawData["data_info"] : [];
if (!preg_match("/[a-zA-z_-]+/", $resourceName)) {
throw new Exception('接口地址错误', COMMON_ERROR_CODE);
//$respData = json_encode(['code' => COMMON_ERROR_CODE,'msg' => '接口地址错误'], JSON_UNESCAPED_UNICODE);
}
break;
case METHOD_DELETE:
/**
* 删除操作
*/
//设置需要调用的资源操作方法
$this->_actionName = $this->methodAction[METHOD_DELETE] . $this->_resourceName;
/**
* 暂时无法通过非url部分获取DELETE请求体, 因此将请求参数放于url中
*/
//获取文章删除参数
/*$rawData = json_decode(file_get_contents("php://input"), true);
$postData = isset($rawData["data_info"]) ? $rawData["data_info"] : [];*/
if (!preg_match("/[a-zA-z]+/", $resourceName)) {
throw new Exception('接口地址错误', COMMON_ERROR_CODE);
//$respData = json_encode(['code' => COMMON_ERROR_CODE,'msg' => '接口地址错误'], JSON_UNESCAPED_UNICODE);
}
$queryString = trim($_SERVER['QUERY_STRING']);
//需要传递给操作方法的查询参数 array
//查询字符串中的键值对数组
$queryParam = [];
if (!empty($queryString) && count($queryString) > 0) {
//请求链接中包含参数,根据参数查找资源
$queryParam = explode('&', $queryString);
}
//将查询字符串依次放入参数数组中
foreach($queryParam as $v){
list($queryKey, $queryVal) = explode('=', $v);
$reqData[$queryKey] = $queryVal;
}
break;
}
return $reqData;
}
}
$restful = new Restful();
$restful->run();