-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-products.php
50 lines (43 loc) · 1.55 KB
/
json-products.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
<?php
// Ruta del archivo JSON
$jsonFilePath = 'products.json';
// Verificar si el archivo JSON existe
if (!file_exists($jsonFilePath)) {
file_put_contents($jsonFilePath, json_encode([]));
}
// Leer el contenido del archivo JSON
$jsonData = file_get_contents($jsonFilePath);
$products = json_decode($jsonData, true);
// Verificar el método HTTP
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
header('Content-Type: application/json');
echo json_encode($products);
exit;
} elseif ($method === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';
$index = $input['index'] ?? -1;
$product = $input['product'] ?? [];
if ($action === 'add') {
$products[] = $product;
} elseif ($action === 'edit' && $index !== -1 && isset($products[$index])) {
$products[$index] = $product;
} elseif ($action === 'delete' && $index !== -1 && isset($products[$index])) {
array_splice($products, $index, 1);
} else {
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Invalid action or index']);
exit;
}
// Guardar los cambios en el archivo JSON
file_put_contents($jsonFilePath, json_encode($products, JSON_UNESCAPED_UNICODE));
header('Content-Type: application/json');
echo json_encode(['status' => 'success']);
exit;
} else {
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
exit;
}
?>