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.

28 lines
812 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 && k($params, 'apikey') !== null) {
  5. $lat = (float)$params['latitude'];
  6. $lng = (float)$params['longitude'];
  7. $key = $params['apikey'];
  8. $weather = \p3k\Weather::weather_for_location($lat, $lng, $key);
  9. if($weather) {
  10. json_response($app, $weather);
  11. } else {
  12. json_response($app, [
  13. 'error' => 'not_found',
  14. 'error_description' => 'No weather information was found for the requested location, or you used an invalid API key'
  15. ]);
  16. }
  17. } else {
  18. json_response($app, [
  19. 'error' => 'invalid_request',
  20. 'error_description' => 'Request was missing parameters'
  21. ], 400);
  22. }
  23. });