api.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * api/api.php
  4. *
  5. * Minimal REST API stub — migrated from the legacy mysql_connect version.
  6. * The original used mysql_connect() which was removed in PHP 7.
  7. * Rewritten to use PDO via config/database.php.
  8. *
  9. * Note: this API has no real endpoints yet. Extend as needed.
  10. */
  11. require_once __DIR__ . '/../config/database.php';
  12. require_once __DIR__ . '/Rest.inc.php';
  13. class API extends REST
  14. {
  15. public string $data = '';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. public function processApi(): void
  21. {
  22. $func = strtolower(trim(str_replace('/', '', $this->_request['rquest'] ?? '')));
  23. if ($func !== '' && method_exists($this, $func)) {
  24. $this->$func();
  25. } else {
  26. $this->response('{"error":"Not found"}', 404);
  27. }
  28. }
  29. private function hello(): void
  30. {
  31. $this->response(json_encode(['message' => 'Hello from Crop Monitor API']), 200);
  32. }
  33. private function test(): void
  34. {
  35. if ($this->get_request_method() !== 'GET') {
  36. $this->response('', 406);
  37. return;
  38. }
  39. $param = $this->_request['var'] ?? '';
  40. $this->response(json_encode(['param' => $param]), 200);
  41. }
  42. }
  43. $api = new API();
  44. $api->processApi();