-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleWeather.php
178 lines (141 loc) · 4.88 KB
/
googleWeather.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
<?php
/**
* Grabs weather data from Google.com's weather API and return a nicely formatted array
*
* @author Ashwin Surajbali
*/
class googleWeather{
/**
* Zipcode
*
* @var int
*/
public $zip;
/**
* Disable or enable caching
*
* @var boolean
*/
public $enable_cache = 0;
/**
* Path to your cache directory
* eg. /www/website.com/cache
*
* @var string
*/
public $cache_path = '';
/**
* Cache expiration time in seconds
* Default: 3600 = 1 Hour
* If the cached file is older than 1 hour, new data is fetched
*
* @var int
*/
public $cache_time = 3600; // 1 hour
/**
* Location of the weather icons on googles server
* They keep changing this, so alter as necessary
*
* @var unknown_type
*/
public $icons_url = 'http://g0.gstatic.com';
/**
* Full location of the cache file
*
* @var string
*/
private $cache_file;
/**
* Location of the google weather api
*
* @var string
*/
private $gweather_api_url = 'http://www.google.com/ig/api?weather=';
/**
* Storage var for data returned from curl request to the google api
*
* @var string
*/
private $raw_data;
/**
* Pull weather information for 'Zipcode' passed in
* If enable_cache = true, data is cached and refreshed every hour
* Weather data is returned in an associative array
*
* @param int $zip
* @return array
*/
public function get_weather_data($zip = null){
$zip = trim($zip);
if (!$zip || strlen($zip) < 5 || !is_numeric($zip)){
die('Invalid zip code.');
}else{
$this->zip = $zip;
}
if ($this->enable_cache && !empty($this->cache_path)){
$this->cache_file = $this->cache_path . '/' . $this->zip;
return $this->load_from_cache();
}
$this->gweather_api_url = $this->gweather_api_url . $this->zip;
if ($this->make_request()){
$xml = new SimpleXMLElement($this->raw_data);
$return_array = array();
$return_array['forecast_info']['city'] = $xml->weather->forecast_information->city['data'];
$return_array['forecast_info']['zip'] = $xml->weather->forecast_information->postal_code['data'];
$return_array['forecast_info']['date'] = $xml->weather->forecast_information->forecast_date['data'];
$return_array['forecast_info']['date_time'] = $xml->weather->forecast_information->current_date_time['data'];
$return_array['current_conditions']['condition'] = $xml->weather->current_conditions->condition['data'];
$return_array['current_conditions']['temp_f'] = $xml->weather->current_conditions->temp_f['data'];
$return_array['current_conditions']['temp_c'] = $xml->weather->current_conditions->temp_c['data'];
$return_array['current_conditions']['humidity'] = $xml->weather->current_conditions->humidity['data'];
$return_array['current_conditions']['icon'] = $this->icons_url . $xml->weather->current_conditions->icon['data'];
$return_array['current_conditions']['wind'] = $xml->weather->current_conditions->wind_condition['data'];
for ($i = 0; $i < count($xml->weather->forecast_conditions); $i++){
$data = $xml->weather->forecast_conditions[$i];
$return_array['forecast'][$i]['day_of_week'] = $data->day_of_week['data'];
$return_array['forecast'][$i]['low'] = $data->low['data'];
$return_array['forecast'][$i]['high'] = $data->high['data'];
$return_array['forecast'][$i]['icon'] = $this->icons_url . $data->icon['data'];
$return_array['forecast'][$i]['condition'] = $data->condition['data'];
}
}
if ($this->enable_cache && !empty($this->cache_path)){
$this->write_to_cache($return_array);
}
return $return_array;
}
private function load_from_cache(){
if (file_exists($this->cache_file)){
$file_time = filectime($this->cache_file);
$now = time();
$diff = ($now-$file_time);
if ($diff <= $this->cache_time){
return unserialize(file_get_contents($this->cache_file));
}
}
}
private function write_to_cache($data){
if (!file_exists($this->cache_path)){
// attempt to make the dir
mkdir($this->cache_path, 0777);
}
if (!file_put_contents($this->cache_file, serialize($data))){
echo "<br />Could not save data to cache. Please make sure your cache directory exists and is writable.<br />";
}
}
private function make_request(){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $this->gweather_api_url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$this->raw_data = curl_exec ($ch);
curl_close ($ch);
if (empty($this->raw_data)){
return false;
}else{
return true;
}
}
}