3 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
7 * Please see the LICENSE file distributed with this source code for further
8 * information regarding copyright and licensing.
10 * Please visit the following links to read about the usage policies and the license of
11 * OpenWeatherMap before using this class:
13 * @see http://www.OpenWeatherMap.org
14 * @see http://www.OpenWeatherMap.org/terms
15 * @see http://openweathermap.org/appid
18 use Cmfcmf\OpenWeatherMap;
19 use Cmfcmf\OpenWeatherMap\AbstractCache;
21 if (file_exists('../vendor/autoload.php')) {
22 // Library is not part of a project. "composer install" was executed directly on this library's composer file.
23 require('../vendor/autoload.php');
25 // Library is part of a project.
26 /** @noinspection PhpIncludeInspection */
27 require('../../../autoload.php');
31 * Example cache implementation.
35 class ExampleCache extends AbstractCache
37 private function urlToPath($url)
39 $tmp = sys_get_temp_dir();
40 $dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
45 $path = $dir . DIRECTORY_SEPARATOR . md5($url);
53 public function isCached($url)
55 $path = $this->urlToPath($url);
56 if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
57 echo "Weather data is NOT cached!\n";
62 echo "Weather data is cached!\n";
70 public function getCached($url)
72 return file_get_contents($this->urlToPath($url));
78 public function setCached($url, $content)
80 file_put_contents($this->urlToPath($url), $content);
84 // Language of data (try your own language here!):
87 // Units (can be 'metric' or 'imperial' [default]):
90 // Example 1: Use your own cache implementation. Cache for 10 seconds only in this example.
91 $owm = new OpenWeatherMap(null, new ExampleCache(), 10);
93 $weather = $owm->getWeather('Berlin', $units, $lang);
94 echo "EXAMPLE 1<hr />\n\n\n";
95 echo $weather->temperature;