|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace artes\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use artes\Curl\Curl; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A class for OpenWeatherMap. |
|
9
|
|
|
* |
|
10
|
|
|
* @SuppressWarnings(PHPMD) |
|
11
|
|
|
*/ |
|
12
|
|
|
class OpenWeather |
|
13
|
|
|
{ |
|
14
|
|
|
private $weatherkey; |
|
15
|
|
|
private $lat; |
|
16
|
|
|
private $long; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor to initiate an OpenWeather object, |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $userinput |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
8 |
|
public function __construct(string $weatherkey, string $lat, string $long) |
|
25
|
|
|
{ |
|
26
|
8 |
|
$this->weatherkey = $weatherkey; |
|
27
|
8 |
|
$this->lat = $lat; |
|
28
|
8 |
|
$this->long = $long; |
|
29
|
8 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
// public function curlcall($url) |
|
32
|
|
|
// { |
|
33
|
|
|
// $ch = curl_init(); |
|
34
|
|
|
// curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url); |
|
35
|
|
|
// curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, 1); |
|
36
|
|
|
// $apiresponse = curl_exec(/** @scrutinizer ignore-type */ $ch); |
|
37
|
|
|
// |
|
38
|
|
|
// $jsonresp = json_decode($apiresponse, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE); |
|
39
|
|
|
// return $jsonresp; |
|
40
|
|
|
// } |
|
41
|
|
|
|
|
42
|
6 |
|
public function currentweather() : array |
|
43
|
|
|
{ |
|
44
|
6 |
|
$mycurl = new Curl; |
|
45
|
6 |
|
$url = "https://api.openweathermap.org/data/2.5/weather?lat=" . $this->lat . "&lon=" . $this->long . "&appid=" . $this->weatherkey . "&units=metric&lang=se"; |
|
46
|
6 |
|
$jsonresp = $mycurl->curl($url); |
|
47
|
6 |
|
return $jsonresp; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
public function forecast() : array |
|
51
|
|
|
{ |
|
52
|
6 |
|
$mycurl = new Curl; |
|
53
|
6 |
|
$url = "https://api.openweathermap.org/data/2.5/onecall?lat=" . $this->lat . "&lon=" . $this->long . "&exclude=minutely,hourly&appid=" . $this->weatherkey . "&units=metric&lang=se"; |
|
54
|
6 |
|
$jsonresp = $mycurl->curl($url); |
|
55
|
6 |
|
return $jsonresp; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// multiple curls serially |
|
59
|
|
|
// public function historicweather() : array |
|
60
|
|
|
// { |
|
61
|
|
|
// $days = $this->generateTimestamps(); |
|
62
|
|
|
// $result = []; |
|
63
|
|
|
// $mh = curl_multi_init(); |
|
64
|
|
|
// for ($i = 0; $i < count($days); $i++) { |
|
65
|
|
|
// $ch = curl_init(); |
|
66
|
|
|
// $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" . $this->lat . "&lon=" . $this->long . "&dt=" . $days[$i]. "&appid=" . $this->weatherkey . "&units=metric&lang=se"; |
|
67
|
|
|
// curl_setopt($ch, CURLOPT_URL, $url); |
|
68
|
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
69
|
|
|
// $apiresponse = curl_exec($ch); |
|
70
|
|
|
// $jsonresp = json_decode($apiresponse, JSON_UNESCAPED_UNICODE); |
|
71
|
|
|
// $result[] = $jsonresp; |
|
72
|
|
|
// curl_close ($ch); |
|
73
|
|
|
// } |
|
74
|
|
|
// return $result; |
|
75
|
|
|
// } |
|
76
|
|
|
|
|
77
|
|
|
// multiple curls parallelly |
|
78
|
6 |
|
public function historicweather() : array |
|
79
|
|
|
{ |
|
80
|
6 |
|
$days = $this->generateTimestamps(); |
|
81
|
6 |
|
$urls = []; |
|
82
|
6 |
|
$mycount = count($days); |
|
83
|
6 |
|
for ($i = 0; $i < $mycount; $i++) { |
|
84
|
6 |
|
$urls[] = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" . $this->lat . "&lon=" . $this->long . "&dt=" . $days[$i]. "&appid=" . $this->weatherkey . "&units=metric&lang=se"; |
|
85
|
|
|
} |
|
86
|
6 |
|
$result = $this->mymulticurl($urls); |
|
87
|
6 |
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
6 |
|
private function getHandles($urls, $multi) |
|
91
|
|
|
{ |
|
92
|
6 |
|
$handles = []; |
|
93
|
6 |
|
foreach ($urls as $url) { |
|
94
|
6 |
|
$ch = curl_init($url); |
|
95
|
6 |
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HEADER, false); |
|
96
|
6 |
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true); |
|
97
|
6 |
|
curl_multi_add_handle($multi, /** @scrutinizer ignore-type */ $ch); |
|
98
|
6 |
|
$handles[$url] = $ch; |
|
99
|
|
|
} |
|
100
|
6 |
|
return [$handles, $multi]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
private function mymulticurl($urls) |
|
104
|
|
|
{ |
|
105
|
6 |
|
$result = []; |
|
106
|
6 |
|
$multi = curl_multi_init(); |
|
107
|
6 |
|
$output = $this->getHandles($urls, $multi); |
|
108
|
6 |
|
$handles = $output[0]; |
|
109
|
6 |
|
$multi = $output[1]; |
|
110
|
|
|
do { |
|
111
|
6 |
|
$mrc = curl_multi_exec($multi, $active); |
|
112
|
6 |
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
113
|
|
|
|
|
114
|
6 |
|
while ($active && $mrc == CURLM_OK) { |
|
115
|
|
|
do { |
|
116
|
6 |
|
$mrc = curl_multi_exec($multi, $active); |
|
117
|
6 |
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
118
|
|
|
} |
|
119
|
6 |
|
foreach ($handles as $channel) { |
|
120
|
6 |
|
$html = curl_multi_getcontent($channel); |
|
121
|
6 |
|
$jsonresp = json_decode($html, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE); |
|
122
|
6 |
|
$result[] = $jsonresp; |
|
123
|
6 |
|
curl_multi_remove_handle($multi, $channel); |
|
124
|
|
|
} |
|
125
|
6 |
|
curl_multi_close($multi); |
|
126
|
6 |
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
6 |
|
private function generateTimestamps($timestamp = null) : array |
|
130
|
|
|
{ |
|
131
|
6 |
|
if (!$timestamp) { |
|
132
|
6 |
|
$timestamp = time(); |
|
133
|
|
|
} |
|
134
|
6 |
|
$day = 60*60*24; |
|
135
|
6 |
|
$result = array(); |
|
136
|
6 |
|
for ($i = 0; $i < 5; $i++) { |
|
137
|
6 |
|
$timestamp -= $day; |
|
138
|
6 |
|
array_push($result, $timestamp); |
|
139
|
|
|
} |
|
140
|
6 |
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|