1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pamo\Weather; |
4
|
|
|
|
5
|
|
|
use Pamo\MultiCurl\MultiCurl; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* IP validator |
9
|
|
|
*/ |
10
|
|
|
class Weather |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array $data to store weather data. |
14
|
|
|
* @var object $multiCurl tool. |
15
|
|
|
* @var array $api all available API's. |
16
|
|
|
* @var string $baseAddress for the API. |
17
|
|
|
* @var string $apiKey for authentication. |
18
|
|
|
* @var array $config for API url. |
19
|
|
|
*/ |
20
|
|
|
private $data; |
21
|
|
|
private $multiCurl; |
22
|
|
|
private $api; |
|
|
|
|
23
|
|
|
private $baseAddress; |
24
|
|
|
private $apiKey; |
25
|
|
|
private $forecastConfig; |
26
|
|
|
private $historyConfig; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Geotag current IP |
32
|
|
|
* |
33
|
|
|
* @param string $baseAddress for the API. |
34
|
|
|
* @param string $apiKey for authentication. |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
1 |
|
public function init() |
38
|
|
|
{ |
39
|
1 |
|
$this->data = []; |
40
|
1 |
|
$this->multiCurl = new MultiCurl(); |
41
|
|
|
|
42
|
1 |
|
$filename = ANAX_INSTALL_PATH . "/config/api.php"; |
43
|
1 |
|
$api = file_exists($filename) ? require $filename : null; |
44
|
|
|
|
45
|
1 |
|
if ($api) { |
46
|
|
|
$this->baseAddress = $api["url"]["weather"]; |
47
|
|
|
$this->apiKey = $api["key"]["weather"]; |
48
|
|
|
} else { |
49
|
1 |
|
$this->baseAddress = getenv("API_URL_WEATHER"); |
50
|
1 |
|
$this->apiKey = getenv("API_KEY_WEATHER"); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$this->forecastConfig = [ |
54
|
|
|
"exclude" => "exclude=[currently,minutely,hourly,alerts,flags]", |
55
|
|
|
"units" => "&units=si" |
56
|
|
|
]; |
57
|
1 |
|
$this->historyConfig = [ |
58
|
|
|
"exclude" => "exclude=[minutely,hourly,currently,flags]", |
59
|
|
|
"units" => "&units=si" |
60
|
|
|
]; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get data with curl. |
67
|
|
|
* |
68
|
|
|
* @param string $lattitude for location. |
69
|
|
|
* @param string $longitude for location. |
70
|
|
|
* @param string $type for weather forecast or weather history. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
1 |
|
public function getAllData(string $lattitude = null, string $longitude = null, string $type = "forecast") : array |
75
|
|
|
{ |
76
|
1 |
|
$forecastConfig = implode("", $this->forecastConfig); |
77
|
1 |
|
$historyConfig = implode("", $this->historyConfig); |
78
|
1 |
|
$apiRequest = []; |
79
|
1 |
|
$day = time(); |
80
|
|
|
|
81
|
1 |
|
if ($type === "forecast") { |
82
|
1 |
|
$apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude?$forecastConfig"; |
83
|
1 |
|
} else if ($type === "history") { |
84
|
1 |
|
for ($i = 1; $i <= 30; $i++) { |
85
|
1 |
|
$day -= 86400; |
86
|
1 |
|
$apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude,$day?$historyConfig"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if ($apiRequest) { |
91
|
1 |
|
$data = $this->multiCurl->get($apiRequest); |
92
|
|
|
|
93
|
1 |
|
if ($data) { |
94
|
1 |
|
foreach ($data as $row) { |
95
|
1 |
|
if (array_key_exists("daily", $row) && array_key_exists("data", $row["daily"])) { |
96
|
1 |
|
array_walk($row["daily"]["data"], array('self', 'build')); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $this->data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Build weather array. |
109
|
|
|
* |
110
|
|
|
* @param string $value |
111
|
|
|
* @param string $key |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
1 |
|
public function build($data) |
115
|
|
|
{ |
116
|
1 |
|
if (array_key_exists("time", $data)) { |
117
|
1 |
|
$time = date('d M Y', $data["time"]); |
118
|
1 |
|
$data["time"] = $time; |
119
|
1 |
|
$this->data[$time] = $data; |
120
|
|
|
} |
121
|
1 |
|
} |
122
|
|
|
} |
123
|
|
|
|