1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Curl, a class supporting the darkssky API. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Anax\Curl; |
7
|
|
|
|
8
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
9
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
10
|
|
|
|
11
|
|
|
class Curl |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use ContainerInjectableTrait; |
15
|
|
|
|
16
|
2 |
|
public function weather($latitude, $longitude) |
17
|
|
|
{ |
18
|
2 |
|
$apiKey = require ANAX_INSTALL_PATH . "/config/apikey.php"; |
19
|
2 |
|
$apiKey = $apiKey["darksky"]; |
20
|
2 |
|
$curl = curl_init(); |
21
|
|
|
|
22
|
2 |
|
curl_setopt($curl, CURLOPT_URL, "https://api.darksky.net/forecast/$apiKey/$latitude,$longitude?exclude=minutely,hourly,alerts,flags"); |
|
|
|
|
23
|
2 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
24
|
|
|
|
25
|
2 |
|
$result = curl_exec($curl); |
|
|
|
|
26
|
|
|
|
27
|
2 |
|
$decode = json_decode($result); |
28
|
|
|
|
29
|
2 |
|
return $decode; |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
public function pastWeather($latitude, $longitude) |
33
|
|
|
{ |
34
|
2 |
|
$apiKey = require ANAX_INSTALL_PATH . "/config/apikey.php"; |
35
|
2 |
|
$apiKey = $apiKey["darksky"]; |
36
|
|
|
|
37
|
2 |
|
$url = "https://api.darksky.net/forecast/$apiKey/$latitude,$longitude"; |
38
|
|
|
|
39
|
|
|
|
40
|
2 |
|
$month = []; |
41
|
2 |
|
for ($i = 0; $i > -30; $i--) { |
42
|
2 |
|
$month[] = strtotime("$i day"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$options = [ |
46
|
2 |
|
CURLOPT_RETURNTRANSFER => true, |
47
|
|
|
]; |
48
|
|
|
|
49
|
2 |
|
$mhd = curl_multi_init(); |
50
|
|
|
|
51
|
2 |
|
$chAll = []; |
52
|
2 |
|
foreach ($month as $day) { |
53
|
2 |
|
$chd = curl_init("$url, $day"); |
54
|
2 |
|
curl_setopt_array($chd, $options); |
|
|
|
|
55
|
2 |
|
curl_multi_add_handle($mhd, $chd); |
|
|
|
|
56
|
2 |
|
$chAll[] = $chd; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$running = null; |
60
|
|
|
|
61
|
|
|
do { |
62
|
2 |
|
curl_multi_exec($mhd, $running); |
63
|
2 |
|
} while ($running); |
64
|
|
|
|
65
|
2 |
|
foreach ($chAll as $chd) { |
66
|
2 |
|
curl_multi_remove_handle($mhd, $chd); |
67
|
|
|
} |
68
|
2 |
|
curl_multi_close($mhd); |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
2 |
|
$response = []; |
73
|
|
|
|
74
|
2 |
|
foreach ($chAll as $chd) { |
75
|
2 |
|
$data = curl_multi_getcontent($chd); |
76
|
2 |
|
$response[] = json_decode($data, true); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
1 |
|
public function getApiKey() |
85
|
|
|
{ |
86
|
|
|
//For rendering openstreetmaps |
87
|
1 |
|
$apiKey = require ANAX_INSTALL_PATH . "/config/apikey.php"; |
88
|
1 |
|
$apiKey = $apiKey["openstreetmap"]; |
89
|
1 |
|
return $apiKey; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|