1
|
|
|
<?php |
2
|
|
|
namespace Anax\Weather; |
3
|
|
|
|
4
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
5
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A sample controller to show how a controller class can be implemented. |
9
|
|
|
* The controller will be injected with $di if implementing the interface |
10
|
|
|
* ContainerInjectableInterface, like this sample class does. |
11
|
|
|
* The controller is mounted on a particular route and can then handle all |
12
|
|
|
* requests for that mount point. |
13
|
|
|
* |
14
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
15
|
|
|
*/ |
16
|
|
|
class Weatherhelper implements ContainerInjectableInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerInjectableTrait; |
19
|
|
|
|
20
|
|
|
protected $apiKey; |
21
|
|
|
|
22
|
4 |
|
public function setKey(string $key) |
23
|
|
|
{ |
24
|
4 |
|
$this->apiKey = $key; |
25
|
4 |
|
} |
26
|
|
|
|
27
|
2 |
|
public function getLocation($ipAddress) |
28
|
|
|
{ |
29
|
2 |
|
$get = curl_init(); |
30
|
2 |
|
curl_setopt($get, CURLOPT_URL, "api.openweathermap.org/data/2.5/forecast?q=" . $ipAddress . "&cnt=7&appid=" . $this->apiKey); |
|
|
|
|
31
|
2 |
|
curl_setopt($get, CURLOPT_RETURNTRANSFER, 1); |
32
|
2 |
|
$result = curl_exec($get); |
|
|
|
|
33
|
2 |
|
$inJson = json_decode($result, true); |
34
|
|
|
|
35
|
|
|
$location = [ |
36
|
2 |
|
"list" => $inJson['list'] ?? null, |
37
|
|
|
]; |
38
|
|
|
|
39
|
2 |
|
return $location; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function getUsersThroughMultiCurl($params, $city) |
43
|
|
|
{ |
44
|
2 |
|
$url = "api.openweathermap.org/data/2.5/find?q=" . $city . "&cnt="; |
45
|
2 |
|
$url2 = "&appid=" . $this->apiKey; |
46
|
|
|
|
47
|
|
|
$options = [ |
48
|
2 |
|
CURLOPT_RETURNTRANSFER => 1, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
// Add all curl handlers and remember them |
52
|
|
|
// Initiate the multi curl handler |
53
|
2 |
|
$mhh = curl_multi_init(); |
54
|
2 |
|
$chAll = []; |
55
|
2 |
|
foreach ($params as $param) { |
56
|
2 |
|
$ch = curl_init($url . $param . $url2); |
57
|
2 |
|
curl_setopt_array($ch, $options); |
|
|
|
|
58
|
2 |
|
curl_multi_add_handle($mhh, $ch); |
|
|
|
|
59
|
2 |
|
$chAll[] = $ch; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Execute all queries simultaneously, |
63
|
|
|
// and continue when all are complete |
64
|
2 |
|
$running = null; |
65
|
|
|
do { |
66
|
2 |
|
curl_multi_exec($mhh, $running); |
67
|
2 |
|
} while ($running); |
68
|
|
|
|
69
|
|
|
// Close the handles |
70
|
2 |
|
foreach ($chAll as $ch) { |
71
|
2 |
|
curl_multi_remove_handle($mhh, $ch); |
72
|
|
|
} |
73
|
2 |
|
curl_multi_close($mhh); |
74
|
|
|
|
75
|
|
|
// All of our requests are done, we can now access the results |
76
|
2 |
|
$response = []; |
77
|
2 |
|
foreach ($chAll as $ch) { |
78
|
2 |
|
$data = curl_multi_getcontent($ch); |
79
|
2 |
|
$response[] = json_decode($data, true); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return $response; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|