-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_items.php
40 lines (32 loc) · 1.22 KB
/
update_items.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
<?php
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Read the JSON data from the request body
$jsonContent = file_get_contents('php://input');
// Decode the JSON data into an associative array
$data = json_decode($jsonContent, true);
// Check if the decoding was successful
if ($data !== null) {
// Update the items.json file with the new data
updateItemsJson($data);
// Send a response
echo json_encode(['success' => true]);
} else {
// Send an error response for invalid JSON data
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON data']);
}
} else {
// Send an error response for unsupported request methods
http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']);
}
// Function to update items.json without merging
function updateItemsJson($data) {
$filePath = 'items.json';
// Encode the new data as JSON
$jsonContent = json_encode($data, JSON_PRETTY_PRINT);
// Save the updated content back to items.json, overwriting the existing content
file_put_contents($filePath, $jsonContent);
}
?>