Browse Source

replace weather provider with openweathermap.org

main
Aaron Parecki 4 years ago
parent
commit
d08a6af502
2 changed files with 54 additions and 49 deletions
  1. +52
    -47
      p3k/Weather.php
  2. +2
    -2
      views/index.php

+ 52
- 47
p3k/Weather.php View File

@ -6,14 +6,16 @@ use DateTime, DateTimeZone;
class Weather { class Weather {
public static function weather_for_location($lat, $lng, $key) { public static function weather_for_location($lat, $lng, $key) {
if($key == 'XXX') {
return ['error' => 'Please provide an api key from openweathermap.org'];
}
$data = self::_fetch($lat, $lng, $key); $data = self::_fetch($lat, $lng, $key);
if(!$data) return null; if(!$data) return null;
if(!property_exists($data, 'currently'))
if(!property_exists($data, 'weather'))
return null; return null;
$current = $data->currently;
$weather = [ $weather = [
'description' => null, 'description' => null,
'icon' => [ 'icon' => [
@ -32,45 +34,42 @@ class Weather {
] ]
]; ];
if($current) {
$sunny = self::_sunny($data->latitude, $data->longitude);
$icon_name = self::_icon_name($current->icon);
$weather['sun'] = $sunny;
$weather['description'] = $current->summary;
$weather['icon']['name'] = $icon_name;
$weather['temp'] = [
'num' => (double)$current->temperature,
'unit' => '°F'
];
$weather['feelslike'] = [
'num' => (double)$current->apparentTemperature,
'unit' => '°F'
];
$weather['wind'] = [
'num' => $current->windSpeed,
'unit' => 'mph'
];
$weather['pressure'] = [
'num' => (int)$current->pressure,
'unit' => 'mb'
];
$weather['humidity'] = [
'num' => round($current->humidity*100),
'unit' => '%'
];
$tz = new DateTimeZone($sunny['timezone']);
$now = new DateTime();
$now->setTimeZone($tz);
$offset = $now->format('Z')/3600;
$weather['timezone']['name'] = $sunny['timezone'];
$weather['timezone']['offset'] = $offset;
}
$sunny = self::_sunny($data->coord->lat, $data->coord->lon);
$icon_name = self::_icon_name($data->weather[0]->id);
$weather['sun'] = $sunny;
$weather['description'] = $data->weather[0]->description;
$weather['icon']['name'] = $icon_name;
$weather['temp'] = [
'num' => (double)$data->main->temp,
'unit' => '°F'
];
$weather['feelslike'] = [
'num' => (double)$data->main->feels_like,
'unit' => '°F'
];
$weather['wind'] = [
'num' => $data->wind->speed,
'unit' => 'mph'
];
$weather['pressure'] = [
'num' => (int)$data->main->pressure,
'unit' => 'mb'
];
$weather['humidity'] = [
'num' => round($data->main->humidity),
'unit' => '%'
];
$tz = new DateTimeZone($data->timezone);
$now = new DateTime();
$now->setTimeZone($tz);
$offset = $now->format('Z')/3600;
$weather['timezone']['name'] = $sunny['timezone'];
$weather['timezone']['offset'] = $offset;
#$weather['raw'] = $current; #$weather['raw'] = $current;
@ -79,11 +78,13 @@ class Weather {
private static function _fetch($lat, $lng, $key) { private static function _fetch($lat, $lng, $key) {
$params = [ $params = [
'exclude' => 'minutely,hourly,daily,alerts,flags',
'units' => 'us',
'units' => 'imperial',
'lat' => $lat,
'lon' => $lng,
'appid' => $key,
]; ];
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.darksky.net/forecast/'.$key.'/'.$lat.','.$lng.'?'.http_build_query($params));
curl_setopt($ch, CURLOPT_URL, 'https://api.openweathermap.org/data/2.5/weather?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// curl_setopt($ch, CURLOPT_USERAGENT, ''); // curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_TIMEOUT, 5);
@ -108,8 +109,8 @@ class Weather {
$offset = $now->format('Z')/3600; $offset = $now->format('Z')/3600;
$now = $now->format('H') + ($now->format('i')/60); $now = $now->format('H') + ($now->format('i')/60);
$sunrise = date_sunrise($now, SUNFUNCS_RET_DOUBLE, $lat, $lng, 108, $offset);
$sunset = date_sunset($now, SUNFUNCS_RET_DOUBLE, $lat, $lng, 108, $offset);
$sunrise = date_sunrise($now, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90, $offset);
$sunset = date_sunset($now, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90, $offset);
return [ return [
'sunrise' => round($sunrise,2), 'sunrise' => round($sunrise,2),
@ -121,6 +122,8 @@ class Weather {
} }
private static function _icon_name($icon) { private static function _icon_name($icon) {
return 'wi-owm-'.$icon;
/*
// A mapping of darksky to weather-icons is here https://erikflowers.github.io/weather-icons/api-list.html // A mapping of darksky to weather-icons is here https://erikflowers.github.io/weather-icons/api-list.html
$map = [ $map = [
'clear-day' => 'day-sunny', 'clear-day' => 'day-sunny',
@ -142,6 +145,8 @@ class Weather {
} else { } else {
return false; return false;
} }
*/
} }
} }

+ 2
- 2
views/index.php View File

@ -32,13 +32,13 @@
<li><a href="/api/weather?latitude=45.5118&amp;longitude=-122.6433&amp;apikey=XXX">/api/weather?latitude=45.5118&amp;longitude=-122.6433&amp;apikey=XXX</a></li> <li><a href="/api/weather?latitude=45.5118&amp;longitude=-122.6433&amp;apikey=XXX">/api/weather?latitude=45.5118&amp;longitude=-122.6433&amp;apikey=XXX</a></li>
</ul> </ul>
<p>You'll need to pass a Dark Sky API key in the request. Icon names reference the <a href="https://erikflowers.github.io/weather-icons/">weather-icons</a> icon font.</p>
<p>You'll need to pass an OpenWeatherMap.org API key in the request. Icon names reference the <a href="https://erikflowers.github.io/weather-icons/">weather-icons</a> icon font.</p>
<h2><i class="fa fa-map"></i> Static Maps</h2> <h2><i class="fa fa-map"></i> Static Maps</h2>
<p><a href="/map/img?marker[]=lat:45.5165;lng:-122.6764;icon:small-blue-cutout&amp;basemap=gray&amp;width=600&amp;height=240&amp;zoom=14">/map/img?marker[]=lat:45.5165;lng:-122.6764;icon:small-blue-cutout&amp;basemap=gray&amp;width=600&amp;height=240&amp;zoom=14</a></p> <p><a href="/map/img?marker[]=lat:45.5165;lng:-122.6764;icon:small-blue-cutout&amp;basemap=gray&amp;width=600&amp;height=240&amp;zoom=14">/map/img?marker[]=lat:45.5165;lng:-122.6764;icon:small-blue-cutout&amp;basemap=gray&amp;width=600&amp;height=240&amp;zoom=14</a></p>
<img src="/assets/sample-map.png" width="600"/> <img src="/assets/sample-map.png" width="600"/>
<p>See <a href="https://github.com/esripdx/Static-Maps-API-PHP/">Static-Maps-API</a> for full API docs</p>
<p>See <a href="https://github.com/aaronpk/Static-Maps-API-PHP/">Static-Maps-API</a> for full API docs</p>
</div> </div>

Loading…
Cancel
Save