1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org . |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* |
7
|
|
|
* Please see the LICENSE file distributed with this source code for further |
8
|
|
|
* information regarding copyright and licensing. |
9
|
|
|
* |
10
|
|
|
* Please visit the following links to read about the usage policies and the license of |
11
|
|
|
* OpenWeatherMap before using this class: |
12
|
|
|
* |
13
|
|
|
* @see http://www.OpenWeatherMap.org |
14
|
|
|
* @see http://www.OpenWeatherMap.org/terms |
15
|
|
|
* @see http://openweathermap.org/appid |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
use Cmfcmf\OpenWeatherMap; |
19
|
|
|
use Cmfcmf\OpenWeatherMap\AbstractCache; |
20
|
|
|
|
21
|
|
|
require_once __DIR__ . '/bootstrap.php'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Example cache implementation. |
25
|
|
|
* |
26
|
|
|
* @ignore |
27
|
|
|
*/ |
28
|
|
|
class ExampleCache extends AbstractCache |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
protected $tmp; |
31
|
|
|
|
32
|
|
View Code Duplication |
private function urlToPath($url) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$tmp = $this->tmp; |
35
|
|
|
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI"; |
36
|
|
|
if (!is_dir($dir)) { |
37
|
|
|
mkdir($dir); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . md5($url); |
41
|
|
|
|
42
|
|
|
return $path; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function isCached($url) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$path = $this->urlToPath($url); |
51
|
|
|
if (!file_exists($path) || filectime($path) + $this->seconds < time()) { |
52
|
|
|
echo "Weather data is NOT cached!\n"; |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
echo "Weather data is cached!\n"; |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function getCached($url) |
66
|
|
|
{ |
67
|
|
|
return file_get_contents($this->urlToPath($url)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function setCached($url, $content) |
74
|
|
|
{ |
75
|
|
|
file_put_contents($this->urlToPath($url), $content); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function setTempPath($path) |
82
|
|
|
{ |
83
|
|
|
if (!is_dir($path)) { |
84
|
|
|
mkdir($path); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->tmp = $path; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Language of data (try your own language here!): |
92
|
|
|
$lang = 'de'; |
93
|
|
|
|
94
|
|
|
// Units (can be 'metric' or 'imperial' [default]): |
|
|
|
|
95
|
|
|
$units = 'metric'; |
96
|
|
|
|
97
|
|
|
// Example 1: Use your own cache implementation. Cache for 10 seconds only in this example. |
98
|
|
|
$cache = new ExampleCache(); |
99
|
|
|
$cache->setTempPath(__DIR__.'/temps'); |
100
|
|
|
$owm = new OpenWeatherMap($myApiKey, null, $cache, 10); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$weather = $owm->getWeather('Berlin', $units, $lang); |
103
|
|
|
echo "EXAMPLE 1<hr />\n\n\n"; |
104
|
|
|
echo $weather->temperature; |
105
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.