-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestServer.php
60 lines (52 loc) · 1.68 KB
/
RestServer.php
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
<?php
class RestServer {
public $global_method;
public $serviceClass;
public function __construct($serviceClass,$method) {
global $METHOD;
$this->serviceClass = $serviceClass;
$global_method = $method;
$METHOD=$method;
}
public function handle() {
global $METHOD;
// if (array_key_exists("method", array_change_key_case($_REQUEST, CASE_LOWER)))
if ($global_method == "") {
$rArray = array_change_key_case($_REQUEST, CASE_LOWER);
//$method = $rArray["method"];
$method = $global_method;
$method = $METHOD;
if (method_exists($this->serviceClass, $method)) {
$ref = new ReflectionMethod($this->serviceClass, $method);
$params = $ref->getParameters();
$pCount = count($params);
$pArray = array();
$paramStr = "";
$i = 0;
foreach ($params as $param) {
$pArray[strtolower($param->getName())] = null;
$paramStr .= $param->getName();
if ($i != $pCount-1) {
$paramStr .= ", ";
}
$i++;
}
foreach ($pArray as $key => $val) {
$pArray[strtolower($key)] = $rArray[strtolower($key)];
}
if (count($pArray) == $pCount && !in_array(null, $pArray)) {
echo call_user_func_array(array($this->serviceClass, $method), $pArray);
}
else {
echo json_encode(array('errorMessage' => "Required parameter(s) for ". $method .": ". $paramStr));
}
}
else {
echo json_encode(array('errorMessage' => "The method " . $method . " does not exist."));
}
}
else {
echo json_encode(array('errorMessage' => 'No method was requested.'));
}
}
}