Browse Source

pass thru wunderground API key

main
Aaron Parecki 6 years ago
parent
commit
926ff2e06f
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 9 additions and 8 deletions
  1. +3
    -2
      controllers/weather.php
  2. +4
    -4
      p3k/Weather.php
  3. +2
    -2
      views/index.php

+ 3
- 2
controllers/weather.php View File

@ -3,12 +3,13 @@
$app->get('/api/weather', function() use($app) { $app->get('/api/weather', function() use($app) {
$params = $app->request()->params(); $params = $app->request()->params();
if(k($params, 'latitude') !== null && k($params, 'longitude') !== null) {
if(k($params, 'latitude') !== null && k($params, 'longitude') !== null && k($params, 'apikey') !== null) {
$lat = (float)$params['latitude']; $lat = (float)$params['latitude'];
$lng = (float)$params['longitude']; $lng = (float)$params['longitude'];
$key = $params['apikey'];
$weather = \p3k\Weather::weather_for_location($lat, $lng);
$weather = \p3k\Weather::weather_for_location($lat, $lng, $key);
if($weather) { if($weather) {
json_response($app, $weather); json_response($app, $weather);

+ 4
- 4
p3k/Weather.php View File

@ -5,8 +5,8 @@ use Config;
use DateTime, DateTimeZone; use DateTime, DateTimeZone;
class Weather { class Weather {
public static function weather_for_location($lat, $lng) {
$data = self::_fetch($lat, $lng);
public static function weather_for_location($lat, $lng, $key) {
$data = self::_fetch($lat, $lng, $key);
if(!$data) return null; if(!$data) return null;
$current = $data->current_observation; $current = $data->current_observation;
@ -87,9 +87,9 @@ class Weather {
return $weather; return $weather;
} }
private static function _fetch($lat, $lng) {
private static function _fetch($lat, $lng, $key) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.wunderground.com/api/'.Config::$wundergroundAPIKey.'/conditions/q/'.$lat.','.$lng.'.json');
curl_setopt($ch, CURLOPT_URL, 'http://api.wunderground.com/api/'.$key.'/conditions/q/'.$lat.','.$lng.'.json');
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);

+ 2
- 2
views/index.php View File

@ -28,10 +28,10 @@
<h3>Retrieving the current weather for a lat/lng</h3> <h3>Retrieving the current weather for a lat/lng</h3>
<ul> <ul>
<li><a href="/api/weather?latitude=45.5118&amp;longitude=-122.6433">/api/weather?latitude=45.5118&amp;longitude=-122.6433</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>Icon names reference the <a href="https://erikflowers.github.io/weather-icons/">weather-icons</a> icon font.</p>
<p>You'll need to pass a Wunderground 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>

Loading…
Cancel
Save