Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. // users.php
  3. header("Content-Type: application/json; charset=UTF-8");
  4. header("Access-Control-Allow-Origin: *");
  5. header("Access-Control-Allow-Methods: GET, POST");
  6. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  7. //require_once 'config.php';
  8. $method = $_SERVER['REQUEST_METHOD'];
  9. switch ($method) {
  10. case 'GET':
  11. // Check if an ID is passed in the query string: /users.php?id=1
  12. if (isset($_GET['id'])) {
  13. $stmt = $pdo->prepare("SELECT id, name, email FROM users WHERE id = ?");
  14. $stmt->execute([$_GET['id']]);
  15. $user = $stmt->fetch();
  16. if ($user) {
  17. echo json_encode($user);
  18. } else {
  19. http_response_code(404);
  20. echo json_encode(["message" => "User not found."]);
  21. }
  22. } else {
  23. // Retrieve all users
  24. $stmt = $pdo->query("SELECT id, name, email FROM users");
  25. $users = $stmt->fetchAll();
  26. echo json_encode($users);
  27. }
  28. break;
  29. case 'POST':
  30. // Retrieve raw JSON data from the request body
  31. $input = json_decode(file_get_contents("php://input"), true);
  32. /*if (!empty($input['name']) && !empty($input['email'])) {
  33. $stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
  34. $stmt->execute([$input['name'], $input['email']]);
  35. http_response_code(201); // Created
  36. echo json_encode([
  37. "message" => "User created successfully.",
  38. "id" => $pdo->lastInsertId()
  39. ]);
  40. } else {
  41. */
  42. http_response_code(400); // Bad Request
  43. echo json_encode(["message" => "Incomplete data. Name and email are required.", "INPUT" => $input ]);
  44. //}
  45. break;
  46. default:
  47. http_response_code(405); // Method Not Allowed
  48. echo json_encode(["message" => "HTTP method not supported."]);
  49. break;
  50. }
  51. ?>