-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
27 lines (24 loc) · 867 Bytes
/
api.php
File metadata and controls
27 lines (24 loc) · 867 Bytes
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
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$data_file = 'projects-data.json';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// Чтение данных
if (file_exists($data_file)) {
echo file_get_contents($data_file);
} else {
echo json_encode(['portfolio' => [], 'vue' => [], 'javascript' => []]);
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Сохранение данных
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if (json_last_error() === JSON_ERROR_NONE) {
file_put_contents($data_file, json_encode($data, JSON_PRETTY_PRINT));
echo json_encode(['message' => 'Data saved successfully']);
} else {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON']);
}
}
?>