|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jiad\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Session\SessionInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Curl |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Make a single curl request to DarkSky's API. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $search object with IP or City to be searched for weather information. |
|
16
|
|
|
* |
|
17
|
|
|
* @return self |
|
18
|
|
|
*/ |
|
19
|
|
|
public function sCurl(array $search) : object |
|
20
|
|
|
{ |
|
21
|
|
|
$key = require(ANAX_INSTALL_PATH . "/config/api_key.php"); |
|
22
|
|
|
$api_key = $key["darksky"]; |
|
23
|
|
|
$lat = $search["lat"]; |
|
24
|
|
|
$long = $search["long"]; |
|
25
|
|
|
|
|
26
|
|
|
$url = "https://api.darksky.net/forecast/$api_key/$lat,$long"; |
|
27
|
|
|
$ch = curl_init(); |
|
28
|
|
|
|
|
29
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
|
|
30
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
31
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
32
|
|
|
|
|
33
|
|
|
$info = curl_exec($ch); |
|
|
|
|
|
|
34
|
|
|
curl_close($ch); |
|
|
|
|
|
|
35
|
|
|
return json_decode($info); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Make multiple curl requests to Darksky's API and requesting weather for 30 days back. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $search object with IP or City to be searched for weather information. |
|
42
|
|
|
* |
|
43
|
|
|
* @return self |
|
44
|
|
|
*/ |
|
45
|
|
|
public function mCurl(array $search) : array |
|
46
|
|
|
{ |
|
47
|
|
|
$key = require(ANAX_INSTALL_PATH . "/config/api_key.php"); |
|
48
|
|
|
$api_key = $key["darksky"]; |
|
49
|
|
|
$lat = $search["lat"]; |
|
50
|
|
|
$long = $search["long"]; |
|
51
|
|
|
|
|
52
|
|
|
$url = "https://api.darksky.net/forecast/$api_key/$lat,$long"; |
|
53
|
|
|
|
|
54
|
|
|
$options = [ |
|
55
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
$mh = curl_multi_init(); |
|
59
|
|
|
$chAll = []; |
|
60
|
|
|
|
|
61
|
|
|
$time = time(); |
|
62
|
|
|
|
|
63
|
|
|
$i = 1; |
|
64
|
|
|
while ($i <= 30) { |
|
65
|
|
|
$ch = curl_init("$url,$time"); |
|
66
|
|
|
curl_setopt_array($ch, $options); |
|
|
|
|
|
|
67
|
|
|
curl_multi_add_handle($mh, $ch); |
|
|
|
|
|
|
68
|
|
|
$chAll[] = $ch; |
|
69
|
|
|
$time = strtotime('-1 day', $time); |
|
70
|
|
|
$i++; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Execute all queries simultaneously, |
|
74
|
|
|
// and continue when all are complete |
|
75
|
|
|
$running = null; |
|
76
|
|
|
do { |
|
77
|
|
|
curl_multi_exec($mh, $running); |
|
78
|
|
|
} while ($running); |
|
79
|
|
|
|
|
80
|
|
|
// Close the handles |
|
81
|
|
|
foreach ($chAll as $ch) { |
|
82
|
|
|
curl_multi_remove_handle($mh, $ch); |
|
83
|
|
|
} |
|
84
|
|
|
curl_multi_close($mh); |
|
85
|
|
|
|
|
86
|
|
|
// All of our requests are done, we can now access the results |
|
87
|
|
|
$response = []; |
|
88
|
|
|
foreach ($chAll as $ch) { |
|
89
|
|
|
$data = curl_multi_getcontent($ch); |
|
90
|
|
|
$response[] = json_decode($data, true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return json_decode(json_encode($response), false); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|