server.php 800 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. function RESTServer()
  3. {
  4. // find the function/method to call
  5. $callback = NULL;
  6. if (preg_match('/rest\/([^\/]+)/i', $_SERVER['REQUEST_URI'], $m)) {
  7. if (isset($GLOBALS['RESTmap'][$_SERVER['REQUEST_METHOD']][$m[1]])) {
  8. $callback = $GLOBALS['RESTmap'][$_SERVER['REQUEST_METHOD']][$m[1]];
  9. }
  10. }
  11. if ($callback) {
  12. // get the request data
  13. $data = NULL;
  14. if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  15. $data = $_GET;
  16. } else if ($tmp = file_get_contents('php://input')) {
  17. $data = json_decode($tmp);
  18. }
  19. $response = call_user_func($callback, $data);
  20. if (is_scalar($response)) {
  21. print $response;
  22. return;
  23. }
  24. print json_encode($response);
  25. }
  26. }