forked from hash-bang/PHP-Wunderground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwunderground.php
217 lines (189 loc) · 6.32 KB
/
wunderground.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Very simple WeatherUnderground.com data retrieval for PHP
* This file should stand alone within any project and can safely be moved to anywhere in your hierarchy.
*
* Feel free to use this class any way you wish.
* Even thinking of not properly crediting the author will result in feelings of guilt and low karma.
*
* @package PHP-Wunderground
* @version 1.0
* @author "Matt Carter" <m@ttcarter.com>
* @link http://hash-bang.net
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class Wunderground {
var $id = "KDCWASHI18";
/**
* The longitude of the area we are querying
* @var float
*/
var $lon = 9.58;
/**
* The latitude of the area we are querying
* @var float
*/
var $lat = 60.10;
/**
* The distance above sea-level (in meters) of the area we are querying
* @var float
*/
var $msl = 70;
/**
* What caching directory to use
* Set this to FALSE to not use any caching
* @var bool|string
*/
var $cache_dir = FALSE;
/**
* The period of life for a cache file
* Files older than this will be removed on subsequent data requests
*/
var $cache_expiry = 3600; // 1 hour
/**
* The last XML object of a forecast request
* @var object
*/
var $forecast_xml;
/**
* Magic method binder to dumbly set the above variables
* e.g. set_cache_dir is bound to $this->cache_dir = $x
* @param string $method The method being called
* @param array $args The arguments being passed to the magic method
*/
function __call($method, $args = null) {
if (substr($method, 0, 4) == 'set_') {
$var = substr($method, 4);
$this->$var = $args[0];
} else {
trigger_error("Unknown method: $method");
}
}
/**
* Perform a retrieval for the Wunderground forecast information
*/
function get_forecast_data($force = FALSE) {
$req = "http://api.yr.no/weatherapi/locationforecast/1.8/?lat={$this->lat};lon={$this->lon};msl={$this->msl}";
if ($this->cache_dir && !$force) {
$cfile = "{$this->cache_dir}/WU-{$this->lat}-{$this->lon}-{$this->msl}.xml";
// Tidy cache
$expiry = mktime() + $this->cache_expiry;
foreach (glob("{$this->cache_dir}/*.xml") as $file)
if (filectime($file) > $expiry)
unlink($file);
if (!file_exists($cfile)) {
$blob = file_get_contents($req);
if (!$blob) die("Invalid return from request to $req");
$fh = fopen($cfile, 'w');
fwrite($fh, $blob);
fclose($fh);
}
$this->forecast_xml = simplexml_load_file($cfile);
} else {
$this->forecast_xml = simplexml_load_file($req);
}
}
function get_history_data($span, $date, $force = FALSE) {
$req = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={$this->id}&graphspan={$span}&month=" . date("m", $date) . "&day=" . date("d", $date) . "&year=" . date("Y", $date) . "&format=1";
if ($this->cache_dir && !$force) {
$cfile = "{$this->cache_dir}/WU-{$this->id}-{$span}-" . date("Y", $date) . "-" . date("m", $date) . "-" . date("d", $date) . ".csv";
// Tidy cache
$expiry = mktime() + $this->cache_expiry;
foreach (glob("{$this->cache_dir}/*.csv") as $file)
if (filectime($file) > $expiry)
unlink($file);
if (!file_exists($cfile)) {
$blob = file_get_contents($req);
if (!$blob) die("Invalid return from request to $req");
$blob = preg_replace('/^\n+|^[\t\s]*\n+/m','', str_replace("<br>", "", $blob));
$fh = fopen($cfile, 'w');
fwrite($fh, $blob);
fclose($fh);
}
}
}
function get_history($span, $date, $force = FALSE) {
$history = array();
// $header = array();
$req = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={$this->id}&graphspan={$span}&month=" . date("m", $date) . "&day=" . date("d", $date) . "&year=" . date("Y", $date) . "&format=1";
if ($this->cache_dir && !$force) {
$cfile = "{$this->cache_dir}/WU-{$this->id}-{$span}-" . date("Y", $date) . "-" . date("m", $date) . "-" . date("d", $date) . ".csv";
// Tidy cache
$expiry = mktime() + $this->cache_expiry;
foreach (glob("{$this->cache_dir}/*.csv") as $file)
if (filectime($file) > $expiry)
unlink($file);
if (!file_exists($cfile)) {
$blob = file_get_contents($req);
if (!$blob) die("Invalid return from request to $req");
$fh = fopen($cfile, 'w');
fwrite($fh, $blob);
fclose($fh);
}
if (($handle = fopen($cfile, "r")) !== FALSE) {
ini_set("auto_detect_line_endings", 1);
$current_row = 1;
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
$num = count($data);
if ($num > 1) {
$row = array();
if ($current_row == 1) {
$header = $data;
} else {
for ($c=0; $c < $num; $c++) {
$row[$header[$c]] = $data[$c];
}
$history[strtotime($row[($span != 'day' ? 'Date' : 'Time')])] = $row;
}
$current_row++;
}
}
fclose($handle);
}
return $history;
}
return $history;
}
/**
* Get the forecast of a specific date
* The date will be rounded backwards to the beginning of the given hour
* So 2011-01-01 15:43 becomes 2011-01-01 15:00
* @param int $epoc The epoc of the date to retrieve
*/
function get_forecast($epoc) {
if (!$this->forecast_xml) die("Called get_forecast() before get_forecast_data()");
$from = date('Y-m-d', $epoc) . 'T' . date('H', $epoc) .':00:00Z';
$info = array('date' => array('epoc' => $epoc, 'iso' => $from));
if (!$casts = $this->forecast_xml->xpath("//time[@from='$from']/location"))
return FALSE;
foreach ($casts as $forecast) {
if ($forecast->xpath("//temperature")) {
foreach ((array) $forecast as $key => $branch) {
if ($key == '@attributes') continue;
$branch = (array) $branch;
$info[$key] = $branch['@attributes'];
}
}
}
return (count($info) == 1) ? FALSE : $info;
}
/**
* Retrieves a range of forecasts from a beginning epoc to the end
* Times are rounded in the same was as get_forecast()
* @param int $start The epoc starting point
* @param int $start The epoc end point
* @param int $step What value to step by. The default is one hour
* @ see get_forecast()
*/
function get_forecast_steps($start, $end, $step = 3600) {
$forecast = array();
$epoc = mktime(date('h', $start), 0, 0, date('m', $start), date('d', $start), date('Y', $start)); // Correct $start point to hour
do {
if ($slice = $this->get_forecast($epoc))
$forecast[$epoc] = $slice;
$epoc += $step;
} while ($epoc < $end);
return $forecast;
}
}
?>