1
|
|
|
<?php |
2
|
|
|
namespace Anax\Model; |
3
|
|
|
|
4
|
|
|
class GeoTag |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
private $config; |
8
|
|
|
|
9
|
1 |
|
public function __construct($config) |
10
|
|
|
{ |
11
|
1 |
|
$this->config = $config; |
12
|
1 |
|
} |
13
|
|
|
|
14
|
1 |
|
public function setConfig($config) |
15
|
|
|
{ |
16
|
1 |
|
$this->config = $config; |
17
|
1 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @param $when -> 7 days in future, or 30 days in the past, $lat -> Latitude, $long -> Longitude |
|
|
|
|
22
|
|
|
* @return array of weather using file_get_contents() |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
public function getWeather($when, $lat, $long) : array |
26
|
|
|
{ |
27
|
|
|
$now = time(); |
28
|
|
|
$dates = array(); |
29
|
|
|
$weather = array(); |
30
|
|
|
$temp = array(); |
31
|
|
|
$time = array(); |
32
|
|
|
|
33
|
|
|
if ($when == "past") { |
34
|
|
|
for ($i = 0; $i < 30; $i++) { |
35
|
|
|
// 24h = 86400 unix time |
36
|
|
|
$now -= 86400; |
37
|
|
|
$dates[] = $now; |
38
|
|
|
} |
39
|
|
|
} else { |
40
|
|
|
for ($i = 0; $i < 7; $i++) { |
41
|
|
|
// 24h = 86400 unix time |
42
|
|
|
$now += 86400; |
43
|
|
|
$dates[] = $now; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
for ($i = 0; $i < count($dates); $i++) { |
|
|
|
|
48
|
|
|
$details = json_decode(file_get_contents("https://api.darksky.net/forecast/{$this->config}/{$lat},{$long},{$dates[$i]}?lang=sv&units=si")); |
49
|
|
|
|
50
|
|
|
$time[] = $details->currently->time; |
51
|
|
|
$weather[] = $details->currently->summary; |
52
|
|
|
$temp[] = $details->currently->temperature; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return [ |
56
|
|
|
"time" => $time, |
57
|
|
|
"weather" => $weather, |
58
|
|
|
"temp" => $temp, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @param $id -> time id of weather 7 days in future, |
|
|
|
|
66
|
|
|
* @param $lat -> Latitude, |
67
|
|
|
* @param $long -> Longitude, |
68
|
|
|
* @return array of weather using multicurl |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
1 |
|
public function getWeatherMultiCurl($when, $lat, $long) : array |
72
|
|
|
{ |
73
|
1 |
|
$dates = []; |
74
|
1 |
|
$now = time(); |
75
|
|
|
|
76
|
1 |
|
if ($when == "past") { |
77
|
1 |
|
for ($i = 0; $i < 30; $i++) { |
78
|
|
|
// 24h = 86400 unix time |
79
|
1 |
|
$now -= 86400; |
80
|
1 |
|
$dates[] = $now; |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
1 |
|
for ($i = 0; $i < 7; $i++) { |
84
|
|
|
// 24h = 86400 unix time |
85
|
1 |
|
$now += 86400; |
86
|
1 |
|
$dates[] = $now; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$url = "https://api.darksky.net/forecast/{$this->config->config}/{$lat},{$long}"; |
91
|
|
|
|
92
|
|
|
$options = [ |
93
|
1 |
|
CURLOPT_RETURNTRANSFER => true, |
94
|
|
|
]; |
95
|
|
|
|
96
|
1 |
|
$mh = curl_multi_init(); |
97
|
1 |
|
$chAll = []; |
98
|
1 |
|
foreach ($dates as $day) { |
99
|
1 |
|
$ch = curl_init("$url,{$day}?lang=sv&units=si"); |
100
|
1 |
|
curl_setopt_array($ch, $options); |
|
|
|
|
101
|
1 |
|
curl_multi_add_handle($mh, $ch); |
|
|
|
|
102
|
1 |
|
$chAll[] = $ch; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// execute all queries simultaneously and countinue when all are complete |
106
|
1 |
|
$running = null; |
107
|
|
|
do { |
108
|
1 |
|
curl_multi_exec($mh, $running); |
109
|
1 |
|
} while ($running); |
110
|
|
|
|
111
|
|
|
// Close handle |
112
|
1 |
|
foreach ($chAll as $ch) { |
113
|
1 |
|
curl_multi_remove_handle($mh, $ch); |
114
|
|
|
} |
115
|
1 |
|
curl_multi_close($mh); |
116
|
|
|
|
117
|
|
|
//req are done, access results |
118
|
1 |
|
$response = []; |
119
|
1 |
|
foreach ($chAll as $ch) { |
120
|
1 |
|
$data = curl_multi_getcontent($ch); |
121
|
1 |
|
$response[] = json_decode($data, true); |
122
|
|
|
|
123
|
|
|
// $weather[] = $response->currently->summary; |
124
|
|
|
// $temp[] = $response->currently->temperature; |
125
|
|
|
} |
126
|
1 |
|
return $response; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|