1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KW\Models; |
4
|
|
|
|
5
|
|
|
class MultiCURL |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array $objekt som api:et returnerar |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
2 |
|
public function __construct($di) |
13
|
|
|
{ |
14
|
2 |
|
$this->di = $di; |
|
|
|
|
15
|
2 |
|
$this->darkSkyKey = $this->di->get("apikeys")["config"]["darkSky"]; |
|
|
|
|
16
|
2 |
|
$this->baseUrl = "https://api.darksky.net/forecast/"; |
|
|
|
|
17
|
2 |
|
$this->apiSettings = "?lang=sv&units=si"; |
|
|
|
|
18
|
2 |
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
2 |
|
public function multicurla($latitude, $longitude) |
22
|
|
|
{ |
23
|
|
|
$result = array( |
24
|
2 |
|
"daily" => array( |
25
|
|
|
"data" => array() |
26
|
|
|
) |
27
|
|
|
); |
28
|
2 |
|
$chh = []; |
29
|
2 |
|
$datum = time(); |
30
|
|
|
|
31
|
|
|
// create all cURL resources |
32
|
2 |
|
for ($i=0; $i < 30; $i++) { |
33
|
2 |
|
$chh[$i] = curl_init(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// set URL and other appropriate options |
37
|
2 |
|
for ($i=0; $i < 30; $i++) { |
38
|
2 |
|
$aktuellsekund = $datum - 30*86400 + $i*86400; |
39
|
2 |
|
$url = ($this->baseUrl . $this->darkSkyKey . "/" . $latitude . "," . $longitude . "," . $aktuellsekund . $this->apiSettings); |
40
|
|
|
// create all cURL resources |
41
|
2 |
|
curl_setopt($chh[$i], CURLOPT_URL, $url); |
42
|
2 |
|
curl_setopt($chh[$i], CURLOPT_HEADER, 0); |
43
|
2 |
|
curl_setopt($chh[$i], CURLOPT_RETURNTRANSFER, 1); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//create the multiple cURL handle |
47
|
2 |
|
$mhh = curl_multi_init(); |
48
|
|
|
|
49
|
|
|
//add the handles |
50
|
2 |
|
for ($i=0; $i < 30; $i++) { |
51
|
2 |
|
curl_multi_add_handle($mhh, $chh[$i]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// execute the handles |
55
|
2 |
|
$running = null; |
56
|
|
|
do { |
57
|
2 |
|
curl_multi_exec($mhh, $running); |
58
|
2 |
|
} while ($running > 0); |
59
|
|
|
|
60
|
|
|
|
61
|
2 |
|
foreach ($chh as $id => $c) { |
62
|
2 |
|
$svar[$id] = curl_multi_getcontent($c); |
63
|
2 |
|
$svar[$id] = json_decode($svar[$id]); |
|
|
|
|
64
|
2 |
|
curl_multi_remove_handle($mhh, $c); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
curl_multi_close($mhh); |
68
|
|
|
|
69
|
2 |
|
for ($i=0; $i < 30; $i++) { |
70
|
2 |
|
if (isset($svar[$i]->daily->data[0])) { |
71
|
|
|
$result['daily']['data'][$i] = $svar[$i]->daily->data[0]; |
72
|
|
|
} else { |
73
|
2 |
|
$result['daily']['data'][$i] = "Historisk väderdata över aktuell plats och tid verkar ej vara tillgänglig"; |
74
|
|
|
//exit("Historisk väderdata över aktuell plats och tid verkar ej vara tillgänglig"); |
75
|
|
|
} |
76
|
|
|
} |
77
|
2 |
|
$result = json_decode(json_encode($result)); |
78
|
2 |
|
return $result; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|