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