-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
53 lines (44 loc) · 1.24 KB
/
Copy pathindex.php
File metadata and controls
53 lines (44 loc) · 1.24 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
<?php
header("Content-Type: application/json");
file_put_contents(
"app.log",
date("Y-m-d H:i:s") . " " .
$_SERVER["REQUEST_METHOD"] . " " .
$_SERVER["REQUEST_URI"] . PHP_EOL,
FILE_APPEND
);
//Start time
$startTime = microtime(true);
$requestMethod = $_SERVER["REQUEST_METHOD"];
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Common response function
function sendResponse($data, $startTime) {
$endTime = microtime(true);
$data["response_time_ms"] = round(($endTime - $startTime) * 1000, 2) . " ms";
echo json_encode($data);
exit;
}
//GET API
if ($requestMethod === "GET" && $uri === "/status") {
sendResponse([
"status" => "ok",
"time" => date("Y-m-d H:i:s"),
"server" => gethostname()
], $startTime);
}
//POST API
if ($requestMethod === "POST" && $uri === "/data") {
$input = json_decode(file_get_contents("php://input"), true);
if (!$input) {
http_response_code(400);
sendResponse(["error" => "Invalid JSON"], $startTime);
}
sendResponse([
"message" => "Data received",
"data" => $input,
"server" => gethostname()
], $startTime);
}
//404
http_response_code(404);
sendResponse(["error" => "Not Found"], $startTime);