-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
54 lines (43 loc) · 1.56 KB
/
index.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
<?php
//allow request from any origin -- for testing only
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode('/api/v1/', $uri); //split /api/v1/ part and resource
//endpoint we are using is test
if (!isset($uri[1]) || $uri[1] != 'test') {
header("HTTP/1.1 404 Not Found");
exit();
}
$request_method = $_SERVER['REQUEST_METHOD'];
$now = date('Y-m-d H:i:s');
if ($request_method === 'GET') {
http_response_code(200);
echo json_encode([
'message' => "Server has received a GET request at $now"]);
} elseif ($request_method === 'POST') {
//read body data
$json = file_get_contents('php://input');
$data = json_decode($json);
$name = $data->name;
http_response_code(201); //code 201
echo json_encode([
'message' => "Server has received a POST request at $now. Name received $name"]);
} elseif ($request_method === 'PATCH') {
//read body data
$json = file_get_contents('php://input');
$data = json_decode($json);
$name = $data->name;
http_response_code(200); //code 200
echo json_encode([
'message' => "Server has received a PATCH request at $now. New name received: $name"]);
} elseif ($request_method === 'DELETE') {
http_response_code(200);
echo json_encode([
'message' => "Server has received a DELETE request at $now"]);
} else {
http_response_code(405);
echo json_encode([
'message' => "Method not allowed at $now"
]);
}