-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.php
More file actions
119 lines (97 loc) · 3.49 KB
/
Copy pathWeather.php
File metadata and controls
119 lines (97 loc) · 3.49 KB
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
<?php
namespace Weather;
class Weather {
private $city = null;
private $country = null;
private $state = null;
public function __construct() {
}
public function get($city=null, $country=null, $state=null){
$this->city = $city;
$this->country = $country;
$this->state = $country;
if (is_null($city)){
$json_string = file_get_contents("http://api.wunderground.com/api/4ba4bf44277e7a9a/conditions/q/icao:EFTP.json");
$parsed_json = json_decode($json_string);
$current = $parsed_json->{'current_observation'};
$location = $current->{'observation_location'}->{'city'};
$temp = "Temp: " . $current->{'temp_c'} . " °C";
$wind = "Wind: " . round($current->{'wind_kph'}/3.6, 2) . " m/s, Direction: " . $current->{'wind_dir'};
$humi = "Humidity: " . $current->{'relative_humidity'};
return "@" . $location . ": " . $temp . ", " . $wind . ", " . $humi;
}
$querypath = "";
if(!is_null($country))
{
$querypath .= rawurlencode($country) . "/";
}
if(!is_null($state))
{
$querypath .= rawurlencode($state) . "/";
}
$querypath .= rawurlencode($city);
$json_string = file_get_contents("http://api.wunderground.com/api/4ba4bf44277e7a9a/geolookup/q/$querypath.json");
$parsed_json = json_decode($json_string);
if(isset($parsed_json->{'response'}->{'results'}))
{
return $this->which_city($parsed_json);
}
else if(isset($parsed_json->{'response'}->{'error'}))
{
return "No data available for " . $city . ", $country $state";
}
else
{
$station_id = "";
foreach($parsed_json->{'location'}->{'nearby_weather_stations'}->{'airport'}->{'station'} as $station)
{
if($station->{'icao'} !== "")
{
$station_id = "icao:" . $station->{'icao'};
break;
}
else
{
foreach($parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'} as $station)
{
$station_id = "pws:" . $station->{'id'};
break;
}
break;
}
}
if($station_id === "")
{
return "No data available for " . $city . ", $country, $state";
}
else
{
$json_string = file_get_contents("http://api.wunderground.com/api/4ba4bf44277e7a9a/conditions/q/$station_id.json");
$parsed_json = json_decode($json_string);
if(isset($parsed_json->{'response'}->{'error'}))
{
return "No data available for " . $city . ", $country $state";
}
else
{
$current = $parsed_json->{'current_observation'};
$location = $current->{'observation_location'}->{'city'};
$temp = "Temp: " . $current->{'temp_c'} . " °C";
$wind = "Wind: " . round($current->{'wind_kph'}/3.6, 2) . " m/s, Direction: " . $current->{'wind_dir'};
$humi = "Humidity: " . $current->{'relative_humidity'};
return "@" . $location . ": " . $temp . ", " . $wind . ", " . $humi;
}
}
}
}
private function which_city($parsed_json)
{
$choices = "Which $this->city? (!saa $this->city country state): ";
foreach($parsed_json->{'response'}->{'results'} as $result)
{
$choices .= trim("\"" . $result->{'country_name'} . " " . $result->{'state'}) . "\"" . ", ";
}
return $choices;
}
}
?>