-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
104 lines (98 loc) · 3.07 KB
/
api.php
File metadata and controls
104 lines (98 loc) · 3.07 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
<?php
class books {
static public function get($id) {
$book = [
'id' => 1234,
'title' => 'PHP Programming',
'price' => 58.8,
'publisher' => [
'publisher' => 'Apress',
'address' => 'Fremont'
],
];
return $book;
}
static public function post($request) {
//echo "Create a Book";
return 1234;
}
static public function put($book, $id) {
//echo "Update a Book";
return $id;
}
static public function delete($book, $id) {
//echo "Delete a Book";
return $id;
}
}
$request = explode('/', $_GET['PATH_INFO']);
array_shift($request);
$resource = array_shift($request);
// only process valid resources
$resources = array('books' => true, 'computers' => true);
if (!array_key_exists($resource, $resources)) {
http_response_code(404);
exit;
}
// route the request to the appropriate function based on method
$method = strtolower($_SERVER["REQUEST_METHOD"]);
switch ($method) {
case 'get':
// Get the Resource
$id = array_shift($request);
$book = books::get($id ); // Returns id of 1234
$json = json_encode($book);
http_response_code(200); // OK
header('Content-Type: application/json');
print $json;
break;
case 'post':
$body = file_get_contents('php://input');
switch (strtolower($_SERVER['HTTP_CONTENT_TYPE'])) {
case "application/json":
$book = json_decode($body);
break;
case "text/xml":
// parsing here
break;
default:
$book = $body;
}
// Validate input
// Create new Resource
//$id = call_user_func(array($resource, $method), $request);
$id = books::post($book); // Returns id of 1234
$json = json_encode(array('id' => $id));
http_response_code(201); // Created
$site = 'localhost';
header("Location: $site/" . $_SERVER['REQUEST_URI'] . "/$id");
header('Content-Type: application/json');
print $json;
break;
case 'put':
$body = file_get_contents('php://input');
switch (strtolower($_SERVER['HTTP_CONTENT_TYPE'])) {
case "application/json":
$book = json_decode($body);
break;
case "text/xml":
// parsing here
break;
default:
//print_r($_SERVER['HTTP_CONTENT_TYPE']);
}
// Validate input
// Modify the Resource
$id = array_shift($request);
$id = books::put($book, $request[0]); // Uses id from request
http_response_code(204); // No conent
break;
case 'delete':
// Delete the Resource
$id = array_shift($request);
$id = books::delete($book, $request[0]); // Uses id from request
http_response_code(204); // No conent
break;
default:
http_response_code(405);
}