Skip to content

Commit 23c2aab

Browse files
author
Alex Seriy
committed
OpenWeatherMap now requires to use the API Key with every API call
from any type of account, including FREE subscription. Currently, the only way to deal with this is to explicitly pass the API key with every invocation of any method of the OpenWeatherMap class. This modification allows to pass the API key to the class contractor and save it as a private member, thus eliminating the need to pass the key to the individual calls. However, for backward compatibility, the API key can be passed to the individual method calls which effectively overrides the API key that persists in the class instance.
1 parent a57d011 commit 23c2aab

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

Examples.ini.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[OpenWeatherMap]
2+
api_key = *******************************
3+

Examples/CurrentWeather.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@
2727
require('../../../autoload.php');
2828
}
2929

30+
// Load the app configuration
31+
$ini = parse_ini_file('../Examples.ini');
32+
$myApiKey = $ini['api_key'];
33+
3034
// Language of data (try your own language here!):
3135
$lang = 'de';
3236

3337
// Units (can be 'metric' or 'imperial' [default]):
3438
$units = 'metric';
3539

3640
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
37-
$owm = new OpenWeatherMap();
41+
$owm = new OpenWeatherMap($myApiKey);
3842

3943
// Example 1: Get current temperature in Berlin.
4044
$weather = $owm->getWeather('Berlin', $units, $lang);
@@ -233,4 +237,8 @@
233237
}
234238

235239
// Example 15: Using an api key:
236-
$owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
240+
echo "<br /><br />\n\n\nEXAMPLE 15<hr />\n\n\n";
241+
$weather = $owm->getWeather('Berlin', $units, $lang, $myApiKey);
242+
echo $weather->temperature . "\n";
243+
echo "<br />\n";
244+

Examples/WeatherForecast.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626
require('../../../autoload.php');
2727
}
2828

29+
// Load the app configuration
30+
$ini = parse_ini_file('../Examples.ini');
31+
$myApiKey = $ini['api_key'];
32+
2933
// Language of data (try your own language here!):
3034
$lang = 'de';
3135

3236
// Units (can be 'metric' or 'imperial' [default]):
3337
$units = 'metric';
3438

3539
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
36-
$owm = new OpenWeatherMap();
40+
$owm = new OpenWeatherMap($myApiKey);
3741

3842
// Example 1: Get forecast for the next 10 days for Berlin.
3943
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 10);

Examples/WeatherHistory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@
2626
require('../../../autoload.php');
2727
}
2828

29+
// Load the app configuration
30+
$ini = parse_ini_file('../Examples.ini');
31+
$myApiKey = $ini['api_key'];
32+
2933
// Language of data (try your own language here!):
3034
$lang = 'en';
3135

3236
// Units (can be 'metric' or 'imperial' [default]):
3337
$units = 'metric';
3438

3539
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
36-
$owm = new OpenWeatherMap();
40+
$owm = new OpenWeatherMap($myApiKey);
3741

3842
// Example 1: Get hourly weather history between 2014-01-01 and today.
3943
$history = $owm->getWeatherHistory('Berlin', new \DateTime('2014-01-01'), new \DateTime('now'), 'hour', $units, $lang);
4044

4145
foreach ($history as $weather) {
4246
echo "Average temperature at " . $weather->time->format('d.m.Y H:i') . ": " . $weather->temperature . "\n\r<br />";
4347
}
48+

0 commit comments

Comments
 (0)