-
Notifications
You must be signed in to change notification settings - Fork 1
/
WundergroundApi.php
160 lines (151 loc) · 5.43 KB
/
WundergroundApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*****************************************************************
* bdWundergroundApi v 1.0 (05-02-2014)
* @copyright Barry Dam - BIC
* Deze weer info maakt gebruik van wunderground.com api
*
* De api query kan een plaatsnaam bevatten of lat lng coordinaten!
*
* bij de apiQuery heeft latlng coordinaten de voorkeur omdat sommige lokale plaatsnamen
* niet herkend worden (bijv Oirsbeek)
*
* de gratis api kan 500 apicalls per dag aan, maximaal 10 per minuut
*
* gebruik door $objWeather = new bdWundergroundApi('Maastricht', apikey, 'NL');
* alles in de public $data is opvraagbaar... en in de arrApiResult staat alles (dus je kunt meer info gebruiken)
*
* LET OP: @use URL_BASE en FILE_PATH !!
*
*******************************************************************/
class bdWundergroundApi {
private $config = array(
'fileCache' => false,
'apiKey' => false,
'apiURL' => false,
'apiLang' => 'NL',
'strFolder' => 'modules/weather/', // folder name relative to root
'strImageFolder' => 'modules/weather/images/' // folder name relative to root
);
/**
* @example $objWeather->strLocation
*/
public $data = array(
'strLocation' => false,
'arrApiResult' => false, // the decoded json from cache or api result
'temperature' => false,
'condition' => false,
'humidity' => false,
'wind' => false,
'image' => false
);
/**
* Magic methods
*/
/**
* @param (string) $getLangAbbr = optional
*/
public function __construct($getLocation = false, $getApiKey = false, $getLangAbbr = false)
{
if (! $getLocation || ! $getApiKey) throw new Exception('Check your clsWeather Params!', 1);
$this->config['apiKey'] = $getApiKey;
$this->strLocation = $getLocation;
if ($getLangAbbr) $this->config['apiLang'] = strtoupper($getLangAbbr);
$this->setData();
}
public function __set($getName, $getValue)
{
$this->data[$getName] = $getValue;
if ($getName == 'strLocation') {
$this->config['fileCache'] = FILE_PATH.$this->config['strFolder'].'cache/json.'.strtolower(preg_replace("/[^a-zA-Z0-9]/", "", $getValue)).'.'.$this->config['apiLang'].'.txt';
$this->config['apiURL'] = 'http://api.wunderground.com/api/'.$this->config['apiKey'].'/conditions/lang:'.$this->config['apiLang'].'/q/'.$getValue.'.json';
}
}
public function __get($getName)
{
if (array_key_exists($getName, $this->data)) {
return $this->data[$getName];
}
}
/**
* setters
*/
private function setData($boolSecondAttempt = false)
{
/**
* first check for cache
*/
if (is_file($this->config['fileCache']) &&
(time() - filemtime($this->config['fileCache'])) < (60*60) // fileage younger than 1 hour
) {
$this->arrApiResult = json_decode(file_get_contents($this->config['fileCache']), true);
}
/**
* get new data
*/
if (! $this->arrApiResult) {
/**
* Curl request
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->config['apiURL']);
curl_setopt($ch, CURLOPT_HEADER, 0);// 1 = headers aan
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$arrCurlResult['data'] = curl_exec($ch);
$arrCurlResult['info'] = curl_getinfo($ch);
$arrCurlResult['error'] = curl_error($ch);
curl_close($ch);
/**
* Process the curl data &nd check for current_observation
*/
$jsonData = ($arrCurlResult['data']) ? json_decode($arrCurlResult['data'], true) : array() ;
if (! empty($jsonData['current_observation'])) {
$this->arrApiResult = $jsonData;
/**
* save to cache file
*/
if ($arrCurlResult['data']) file_put_contents($this->config['fileCache'], $arrCurlResult['data']);
} else if($boolSecondAttempt) {
/**
* Don't throw an exception but just log the error
*/
file_put_contents(FILE_PATH.$this->config['strFolder'].'cache/error'.time().'.txt', implode($arrCurlResult));
} else {
/* try an second attempt (force the cache or redo a curl request) */
if (is_file($this->config['fileCache'])) touch($this->config['fileCache']);
$this->setData(true);
}
}
/**
* set the data
*/
if (! $this->arrApiResult) return ;
$arrObservation = $this->arrApiResult['current_observation'];
$this->temperature = round($arrObservation['temp_c']).'°';
$this->condition = $arrObservation['weather'];
$this->image = URL_BASE.$this->config['strImageFolder'].self::setData_image($arrObservation['icon']);
$this->humidity = $arrObservation['relative_humidity'];
$this->wind = $arrObservation['wind_kph'].' km/h - '.$arrObservation['wind_dir'];
}
private static function setData_image($getStrIcon = false)
{
if (! $getStrIcon) return false;
$arrTypes = array(
'thunder' => array('tstorms'),
'rain' => array('rain'),
'snow' => array('chanceflurries','flurries','chancesleet','sleet','chancesnow','snow'),
'fog' => array('fog'),
'cloudy' => array('cloudy'),
'mostly-cloudy' => array('mostlycloudy'),
'partly-cloudy' => array('partlycloudy'),
'clear-ish' => array('chancerain','chancetstorms'),
'clear-sunny' => array('clear','mostlysunny','partlysunny'),
'sunny' => array('hazy','sunny')
);
foreach ($arrTypes as $strImage => $arrType) {
if (in_arraY($getStrIcon, $arrType)) return $strImage.'.png';
}
return 'default.png';
}
};
?>