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