require 'config/db.php'; require 'model/user.php'; require 'model/message.php'; $app = new Slim\App(); $app->get('/', function ($request, $response, $args) { $response->write("Welcome to Slim!"); return $response; }); $app->get('/hello[/{name}]', function ($request, $response, $args) { $response->write("Hello, " . $args['name']); return $response; })->setArgument('name', 'World!'); $app->get('/user/{id}', function ($request, $response, $args) { $newResponse = $response->withHeader('Content-type', 'application/json'); $newResponse->write(json_encode(getUser(getDbConnection(), $args['id']))); return $newResponse; }); $app->get('/messages', function ($request, $response, $args) { $newResponse = $response->withHeader('Content-type', 'application/json'); $queryParams = $request->getQueryParams(); if (isset($queryParams['lat']) || isset($queryParams['lng'])) { $lat = $queryParams['lat']; $lng = $queryParams['lng']; $messages = messagesWithLatLng(getDbConnection(), $lat, $lng); } else { $messages = messages(getDbConnection()); } $newResponse->write(json_encode($messages)); return $newResponse; }); $app->run();
} function messagesWithLatLng($conn, $lat, $lng) { $sql = "CALL GetMessagesByMyLocation({$lat},{$lng})"; $result = $conn->query($sql); $arr = array(); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $one = null; $one['id'] = $row["id"]; $one['message'] = $row["message"]; $one['lat'] = $row["lat"]; $one['lng'] = $row["lng"]; $one['distance_from_my_location'] = $row["distance_from_my_location"]; $one['created_at'] = $row["created_at"]; $one['user']['name'] = $row["name"]; $one['user']['facebook_id'] = $row["facebook_id"]; $arr[] = $one; } // $json_result['messages'] = $arr; } return $arr; } // main if (isset($_GET['lat']) && isset($_GET['lng'])) { $arr = messagesWithLatLng($conn, $_GET['lat'], $_GET['lng']); } else { $arr = messages($conn); } $conn->close(); echo json_encode($arr);