-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.php
52 lines (39 loc) · 1.24 KB
/
server.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
<?php
/*
* Example server: don't use it in production!
*/
$token = 'your-token';
$groups = [
'about' => ['img', 'title', 'body'],
'openings' => ['title', 'description', 'week-days', 'week-hours', 'weekend-days', 'weekend-hours'],
];
function response($data, int $http_code = 200) {
http_response_code($http_code);
header('Content-Type: application/json');
header("Cache-Control: no-cache, must-revalidate");
echo json_encode($data);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$header = getallheaders()['Authorization'];
$expected = sprintf('Bearer %s', $token);
if ($expected !== $header) {
response('Authorization header is missing or invalid', 403);
}
$request = file_get_contents('php://input');
try {
$payload = json_decode($request, true);
} catch (\Exception $e) {
response('Invalid JSON payload', 422);
}
$group = $_GET['group'] ?? 'default';
if (false === array_key_exists($group, $groups)) {
response('Invalid group', 422);
}
$keys = $groups[$group];
$values = array_filter($payload, function ($item) use ($keys) {
return in_array($item['name'], $keys);
});
response($values, 200);
}
response('It works!', 200);