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.

27 lines
711 B

  1. <?php
  2. $app->get('/api/weather', function() use($app) {
  3. $params = $app->request()->params();
  4. if(k($params, 'latitude') !== null && k($params, 'longitude') !== null) {
  5. $lat = (float)$params['latitude'];
  6. $lng = (float)$params['longitude'];
  7. $weather = \p3k\Weather::weather_for_location($lat, $lng);
  8. if($weather) {
  9. json_response($app, $weather);
  10. } else {
  11. json_response($app, [
  12. 'error' => 'not_found',
  13. 'error_description' => 'No weather information was found for the requested location'
  14. ]);
  15. }
  16. } else {
  17. json_response($app, [
  18. 'error' => 'invalid_request',
  19. 'error_description' => 'Request was missing parameters'
  20. ], 400);
  21. }
  22. });