-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetweatherscript.php
executable file
·216 lines (184 loc) · 6.06 KB
/
getweatherscript.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
#!/user/bin/php
/* CB'S GET THE WEATHER SCRIPT
This script accesses the Wunderground API to get current weather conditions in
various places and document those in the fishingtrips.weather table
The intention is to later combine this information with the fishing trip information
to enhance both.
NOTE: The Wunderground API only allows 10 calls per minute-
each item in the $cities array below counts as one call!
BE CAREFUL!
*/
$cities=array(
"Itasca"=>"http://api.wunderground.com/api/681a9b949662ace6/geolookup/conditions/q/IL/Itasca.json",
"Batavia"=>"http://api.wunderground.com/api/681a9b949662ace6/geolookup/conditions/q/IL/Batavia.json",
"Kalamazoo"=>"http://api.wunderground.com/api/681a9b949662ace6/geolookup/conditions/q/MI/Kalamazoo.json",
"RockfordMI"=>"http://api.wunderground.com/api/681a9b949662ace6/geolookup/conditions/q/MI/Rockford.json"
);
foreach($cities as $val)
{
$json_url = $val;
print "<h1>The URL we're using now is $val</h1><BR>";
$json_string = file_get_contents($json_url);
$parsed_json = json_decode($json_string);
$weathercity = $parsed_json->{'location'}->{'city'};
$weatherlat = $parsed_json->{'location'}->{'lat'};
$weatherlon = $parsed_json->{'location'}->{'lon'};
$weatherzip = $parsed_json->{'location'}->{'zip'};
$weatherstate = $parsed_json->{'location'}->{'state'};
$weathertime = $parsed_json->{'current_observation'}->{'observation_epoch'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
$windchill = $parsed_json->{'current_observation'}->{'windchill_f'};
$heatindex = $parsed_json->{'current_observation'}->{'heat_index_f '};
$feelslike = $parsed_json->{'current_observation'}->{'feelslike_f'};
$pressure_in = $parsed_json->{'current_observation'}->{'pressure_in'};
$pressuretrend = $parsed_json->{'current_observation'}->{'pressure_trend'};
$weather = $parsed_json->{'current_observation'}->{'weather'};
$relhumidity = $parsed_json->{'current_observation'}->{'relative_humidity'};
$visibility = $parsed_json->{'current_observation'}->{'visibility_mi'};
$precip_today = $parsed_json->{'current_observation'}->{'precip_today_in'};
$wind = $parsed_json->{'current_observation'}->{'wind_string'};
$wind_dir = $parsed_json->{'current_observation'}->{'wind_dir'};
$wind_degrees = $parsed_json->{'current_observation'}->{'wind_degrees'};
$wind_mph = $parsed_json->{'current_observation'}->{'wind_mph'};
$wind_gust_mph = $parsed_json->{'current_observation'}->{'wind_gust_mph'};
// make the observation time nice
$nicedate = date("Y-m-d", $weathertime);
$weatherdate = date("Y-m-d");
print "Weatherdate is $weatherdate<BR>";
$weatherhms = date("H:i:s");
print "Weather HMS is $weatherhms<BR>";
// get the unixtime (from the API)
print "The unix time \$weathertime from the API is $weathertime<BR>";
// convert that into hour:minute:second
$hms = date("H:i:s", $weathertime);
print "The hms time is $hms<BR>";
// make that into an integer
$hour = (int)$hms;
print "the integer time is $hour<BR>";
print "The hour is $hour<BR>";
$t = $hour;
switch ($t) {
case $t >= 4 && $t < 8: // between 4am and 8am
$timeofday = "morning"; // it's MORNING
break;
case $t >= 8 && $t < 12: // between 8am and 12pm
$timeofday = "late morning"; // it's LATE MORNING
break;
case $t >= 12 && $t < 14: // between noon and 2pm
$timeofday = "noon"; // it's LNOON
break;
case $t >= 14 && $t < 18: // between 2 and 6pm
$timeofday = "afternoon"; // afternoon
break;
case $t >= 18 && $t < 20: // between 6 and 8pm
$timeofday = "evening"; // evening
break;
case $t >= 20 && $t < 25: // between 10pm and 12am
$timeofday = "night"; // night
break;
case $t >= 0 && $t < 4: // between 10pm and 12am
$timeofday = "night"; // night
break;
}
// run the weather getter at
// 12am, 2am, 5am, 8am, 12pm, 2pm, 7pm, 9pm
print "Time is $nicedate<br><br>";
print "Time of day is is $timeofday<br><br>";
print "<BR> City : $weathercity, $weatherstate <br>
zip : $weatherzip <br>
lat/long : $weatherlat, $weatherlon<BR>
Temp : $temp_f °F <BR>
Windchill : $windchill °F<BR>
Heat index : $heatindex <BR>
Pressure (in) : $pressure_in <BR>
Pressure trend : $pressuretrend <br>
Weather: $weather<BR>
Relative humidity: $relhumidity <BR>
Feels like : $feelslike °F<BR>
Visibility : $visibility miles<BR>
Precipitation today : $precip_today inches<BR>
Wind : $wind <br>
Wind direction : $wind_dir <BR>
Wind speed : $wind_mph <BR>
wind gust : $wind_gust_mph <BR>
<br>
<i>$weathertime<BR></i>
";
// put this stuff in the database
// Connect to the server
mysql_connect("mysql.cbfishes.com","cbfishescom","6thZcnd!") or die(mysql_error());
print "<BR><div id='debug'>Connected to mysql </div>";
// Choose the database
mysql_select_db("fishingtrips") or die(mysql_error());
print "<BR><div id='debug'>Connected to Database</div>";
// PUT THE STUFF INTO THE DATABASE
mysql_query(
"INSERT INTO weather(
weathercity,
weatherlat,
weatherlon,
weatherzip,
weatherstate,
weatherdate,
weatherhms,
timeofday,
temp_f,
windchill,
heatindex,
feelslike,
pressure_in,
pressuretrend,
weather,
relhumidity,
visibility,
precip_today,
wind,
wind_dir,
wind_degrees,
wind_mph,
wind_gust_mph)
VALUES
('$weathercity',
'$weatherlat',
'$weatherlon',
'$weatherzip',
'$weatherstate',
'$weatherdate',
'$weatherhms',
'$timeofday',
'$temp_f',
'$windchill',
'$heatindex',
'$feelslike',
'$pressure_in',
'$pressuretrend',
'$weather',
'$relhumidity',
'$visibility',
'$precip_today',
'$wind',
'$wind_dir',
'$wind_degrees',
'$wind_mph',
'$wind_gust_mph'
)")
or die(mysql_error());
// print "<hr><hr>";
// print "contents of the json <br>";
//
//
// function jsonextract($inputjson)
// {
// $jsonIterator = new RecursiveIteratorIterator(
// new RecursiveArrayIterator(json_decode($inputjson, TRUE)),
// RecursiveIteratorIterator::SELF_FIRST);
//
// foreach ($jsonIterator as $key => $val) {
// if(is_array($val)) {
// echo "$key:\n <br>";
// } else {
// echo "$key => $val\n <br>";
// }
// }
// }
}