action_facultad.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?
  2. $information = [
  3. 'GET' => [],
  4. ];
  5. header('Content-Type: application/json charset=utf-8');
  6. $ruta = "../";
  7. require_once "../class/c_login.php";
  8. // check if the session is started
  9. if (!isset($_SESSION['user'])) {
  10. http_response_code(500);
  11. echo json_encode([
  12. 'error' => 'No se ha iniciado sesión'
  13. ]);
  14. exit;
  15. }
  16. $user = unserialize($_SESSION['user']);
  17. try {
  18. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  19. // check parameters
  20. array_walk($information['GET'], function ($value) {
  21. if (!array_key_exists($value, $_GET)) {
  22. http_response_code(400);
  23. echo json_encode(['error' => "$value is required"]);
  24. exit;
  25. }
  26. });
  27. // step 1: get subrutas
  28. $data = $db->get('facultad');
  29. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  30. } else {
  31. http_response_code(405);
  32. echo json_encode(['error' => 'method not allowed']);
  33. exit;
  34. }
  35. } catch (PDOException $th) {
  36. http_response_code(500);
  37. echo json_encode([
  38. 'error' => $th->getMessage(),
  39. 'query' => $db->getLastQuery(),
  40. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  41. exit;
  42. } catch (Exception $th) {
  43. http_response_code(500);
  44. echo json_encode([
  45. 'error' => $th->getMessage(),
  46. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  47. exit;
  48. }