1 | <?php |
||
2 | namespace Jodn14\Curl; |
||
3 | |||
4 | /** |
||
5 | * Class for wrapping curl. |
||
6 | * If time: { implements CurlInterface } |
||
7 | */ |
||
8 | class Curl |
||
9 | { |
||
10 | /** |
||
11 | * @var string $weatherKey API Key for Dark Sky. |
||
12 | * @var string $ipstackKey API Key for ipstack. |
||
13 | * |
||
14 | */ |
||
15 | |||
16 | private $weatherKey; |
||
17 | private $ipstackKey; |
||
18 | private $mapquestKey; |
||
19 | |||
20 | |||
21 | /** |
||
22 | * Set the weather api key. |
||
23 | * |
||
24 | * @param string $weatherKey to set the weather key. |
||
25 | * @param string $ipstackKey to set the ipstack key. |
||
26 | * @param string $mapquestKey to set the ipstackKey key. |
||
27 | * |
||
28 | * @return self |
||
29 | */ |
||
30 | 6 | public function setKeys($weatherKey, $ipstackKey, $mapquestKey) |
|
31 | { |
||
32 | 6 | $this->weatherKey = $weatherKey; |
|
33 | 6 | $this->ipstackKey = $ipstackKey; |
|
34 | 6 | $this->mapquestKey = $mapquestKey; |
|
35 | 6 | } |
|
36 | |||
37 | |||
38 | /** |
||
39 | * Call weather service to get upcoming 7 days |
||
40 | * |
||
41 | * @param string $lat |
||
42 | * @param string $long |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | 3 | public function getForecast($lat, $long) |
|
47 | { |
||
48 | 3 | $data = array(); |
|
49 | |||
50 | 3 | $url = 'https://api.darksky.net/forecast/'.$this->weatherKey.'/'.$lat.','.$long.'?units=si&lang=sv&exclude=minutely'; |
|
51 | 3 | array_push($data, $url); |
|
52 | |||
53 | // Call multi-curl function with populated url array |
||
54 | 3 | $res = $this->multi($data); |
|
55 | |||
56 | $filtered = [ |
||
57 | 3 | "currently" => $res[0]["currently"]["summary"] ?? null, |
|
58 | 3 | "temp" => round($res[0]["currently"]["temperature"]) ?? null, |
|
59 | 3 | "feels" => round($res[0]["currently"]["apparentTemperature"]) ?? null, |
|
60 | 3 | "wind" => round($res[0]["currently"]["windSpeed"], 1) ?? null, |
|
61 | 3 | "gust" => round($res[0]["currently"]["windGust"], 1) ?? null, |
|
62 | 3 | "senare" => $res[0]["hourly"]["summary"] ?? null, |
|
63 | 3 | "weekly" => $res[0]["daily"]["summary"] ?? null, |
|
64 | ]; |
||
65 | |||
66 | 3 | return $filtered; |
|
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Call weather service to get past 30 days. |
||
72 | * |
||
73 | * @param string $lat |
||
74 | * @param string $long |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | 3 | public function getPast($lat, $long) |
|
79 | { |
||
80 | // Get current time in unix and add to url in for loop |
||
81 | 3 | $data = array(); |
|
82 | 3 | $today = time(); |
|
83 | |||
84 | 3 | for ($i = 0; $i < 30; $i++) { |
|
85 | 3 | $url = 'https://api.darksky.net/forecast/'.$this->weatherKey.'/'.$lat.','.$long.','.$today.'?units=si&lang=sv&exclude=minutely'; |
|
86 | 3 | array_push($data, $url); |
|
87 | 3 | $today -= 24*60*60; |
|
88 | } |
||
89 | |||
90 | // Call multi-curl function with populated url array |
||
91 | 3 | $res = $this->multi($data); |
|
92 | |||
93 | 3 | $filtered = []; |
|
94 | 3 | for ($i = 0; $i < count($res); $i++) { |
|
0 ignored issues
–
show
|
|||
95 | 3 | $filtered[$i]["summary"] = $res[$i]["currently"]["summary"]; |
|
96 | 3 | $filtered[$i]["temperature"] = round($res[$i]["currently"]["temperature"]); |
|
97 | 3 | $filtered[$i]["windSpeed"] = round($res[$i]["currently"]["windSpeed"], 1); |
|
98 | 3 | $filtered[$i]["time"] = date("Y-m-d", $res[$i]["currently"]["time"]); |
|
99 | }; |
||
100 | |||
101 | 3 | return $filtered; |
|
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Call weather service with a multi handle |
||
107 | * |
||
108 | * @param array $data |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | 6 | public function multi($data) |
|
113 | { |
||
114 | 6 | $multiCurl = array(); |
|
115 | 6 | $resultSet = array(); |
|
116 | |||
117 | 6 | $mhandle = curl_multi_init(); |
|
118 | |||
119 | 6 | foreach ($data as $id => $d) { |
|
120 | // Initiate curl for each id |
||
121 | 6 | $multiCurl[$id] = curl_init(); |
|
122 | |||
123 | // Set options and add handle to url |
||
124 | 6 | curl_setopt($multiCurl[$id], CURLOPT_URL, $d); |
|
125 | 6 | curl_setopt($multiCurl[$id], CURLOPT_HEADER, 0); |
|
126 | 6 | curl_setopt($multiCurl[$id], CURLOPT_RETURNTRANSFER, true); |
|
127 | 6 | curl_multi_add_handle($mhandle, $multiCurl[$id]); |
|
128 | } |
||
129 | |||
130 | 6 | $running = null; |
|
131 | do { |
||
132 | 6 | curl_multi_exec($mhandle, $running); |
|
133 | 6 | } while ($running > 0); |
|
134 | |||
135 | 6 | foreach ($multiCurl as $id => $d) { |
|
136 | 6 | $resultSet[$id] = curl_multi_getcontent($d); |
|
137 | 6 | $resultSet[$id] = json_decode($resultSet[$id], true); |
|
138 | 6 | curl_multi_remove_handle($mhandle, $d); |
|
139 | } |
||
140 | |||
141 | 6 | curl_multi_close($mhandle); |
|
142 | |||
143 | 6 | return $resultSet; |
|
144 | } |
||
145 | |||
146 | |||
147 | |||
148 | /** |
||
149 | * Call map API service to get long lat values out of a city name |
||
150 | * |
||
151 | * @param string $search "city name". |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | 6 | public function getCoordinates($search) |
|
156 | { |
||
157 | |||
158 | 6 | $url = 'http://open.mapquestapi.com/nominatim/v1/search.php?key='.$this->mapquestKey.'&format=json&q='.$search.'&limit=1'; |
|
159 | |||
160 | 6 | $apiCall = curl_init($url); |
|
161 | 6 | curl_setopt($apiCall, CURLOPT_RETURNTRANSFER, true); |
|
162 | |||
163 | 6 | $json = curl_exec($apiCall); |
|
164 | 6 | curl_close($apiCall); |
|
165 | |||
166 | 6 | $apiResult = json_decode($json, true); |
|
167 | |||
168 | 6 | if (empty($apiResult)) { |
|
169 | $apiResult = [ |
||
170 | 6 | "code" => 400, |
|
171 | "error" => "Invalid Location, could not find data", |
||
172 | ]; |
||
173 | 6 | return $apiResult; |
|
174 | } |
||
175 | |||
176 | 6 | return $apiResult[0]; |
|
177 | } |
||
178 | } |
||
179 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: