1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asti\Weather; |
4
|
|
|
|
5
|
|
|
use Asti\Weather\CurlService; |
6
|
|
|
use Asti\Weather\ApiDataProcessing; |
7
|
|
|
|
8
|
|
|
class WeatherService |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
* |
14
|
|
|
* curls api, get_file_contents |
15
|
|
|
* sends response to model |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
private $key = null; |
20
|
|
|
private $url = null; |
21
|
|
|
|
22
|
2 |
|
public function setKey($key) |
23
|
|
|
{ |
24
|
2 |
|
$this->key = $key; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
public function setUrl($url) : void |
28
|
|
|
{ |
29
|
2 |
|
$this->url = $url; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function getKey() : string |
33
|
|
|
{ |
34
|
2 |
|
return $this->key; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function getUrl() : string |
38
|
|
|
{ |
39
|
2 |
|
return $this->url; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
2 |
|
public function curlWeatherApi($lon, $lat) : array |
45
|
|
|
{ |
46
|
2 |
|
$curl = new CurlService(); |
47
|
2 |
|
$data = new ApiDataProcessing(); |
48
|
2 |
|
$res = $curl->getDataThroughCurl($this->getUrl() . "?" . "lat=" . $lat . "&lon=" . $lon . "&units=metric" . "&lang=sv" . "&appid=" . $this->getKey()); |
49
|
2 |
|
error_log(gettype($res)); |
50
|
2 |
|
if (isset($res["cod"])) { |
51
|
|
|
return [ |
52
|
|
|
"Error" => "Platsangivelse är fel" |
53
|
|
|
]; |
54
|
|
|
} else { |
55
|
|
|
$json = [ |
56
|
2 |
|
"CurrentTemp" => substr($res["current"]["temp"], 0, 5), |
57
|
2 |
|
"CurrentFeelsLike" => substr($res["current"]["feels_like"], 0, 5), |
58
|
2 |
|
"CurrentWeather" => $res["current"]["weather"][0]["description"], |
59
|
2 |
|
"DailyDates" => $data->loopThroughDate($res["daily"]), |
60
|
2 |
|
"DailyTemperatures" => $data->loopThroughTemp($res["daily"], "temp", "day"), |
61
|
2 |
|
"DailyFeelsLike" => $data->loopThroughTemp($res["daily"], "feels_like", "day"), |
62
|
2 |
|
"DailyDescriptions" => $data->loopThroughDesc($res["daily"], "weather", "description") |
63
|
|
|
]; |
64
|
2 |
|
return [$json]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function curlOldWeatherApi($lon, $lat) : array |
69
|
|
|
{ |
70
|
2 |
|
$curl = new CurlService(); |
71
|
2 |
|
$dateArray = []; |
72
|
2 |
|
$currentTime = time(); |
73
|
2 |
|
for ($x = 0; $x <= 4; $x++) { |
74
|
2 |
|
array_push($dateArray, $currentTime -= 86400); |
75
|
|
|
} |
76
|
2 |
|
$res = $curl->getMultipleCurls($this->getUrl() . "/timemachine?" . "lat=" . $lat . "&lon=" . $lon . "&units=metric" . "&lang=sv" . "&dt=", $dateArray, "&appid=" . $this->getKey()); |
77
|
2 |
|
if (isset($res["cod"])) { |
78
|
|
|
return [ |
79
|
|
|
"Error" => "Väder kan inte ges för positionen. Försök igen" |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
$json = [ |
83
|
2 |
|
"Date1" => date("Y-m-d", $res[0]["current"]["dt"]), |
84
|
2 |
|
"CurrentTemp1" => substr($res[0]["current"]["temp"], 0, 5), |
85
|
2 |
|
"CurrentFeelsLike1" => substr($res[0]["current"]["feels_like"], 0, 5), |
86
|
2 |
|
"CurrentWeather1" => $res[0]["current"]["weather"][0]["main"], |
87
|
2 |
|
"Date2" => date("Y-m-d", $res[1]["current"]["dt"]), |
88
|
2 |
|
"CurrentTemp2" => substr($res[1]["current"]["temp"], 0, 5), |
89
|
2 |
|
"CurrentFeelsLike2" => substr($res[1]["current"]["feels_like"], 0, 5), |
90
|
2 |
|
"CurrentWeather2" => $res[1]["current"]["weather"][0]["main"], |
91
|
2 |
|
"Date3" => date("Y-m-d", $res[2]["current"]["dt"]), |
92
|
2 |
|
"CurrentTemp3" => substr($res[2]["current"]["temp"], 0, 5), |
93
|
2 |
|
"CurrentFeelsLike3" => substr($res[2]["current"]["feels_like"], 0, 5), |
94
|
2 |
|
"CurrentWeather3" => $res[2]["current"]["weather"][0]["main"], |
95
|
2 |
|
"Date4" => date("Y-m-d", $res[3]["current"]["dt"]), |
96
|
2 |
|
"CurrentTemp4" => substr($res[3]["current"]["temp"], 0, 5), |
97
|
2 |
|
"CurrentFeelsLike4" => substr($res[3]["current"]["feels_like"], 0, 5), |
98
|
2 |
|
"CurrentWeather4" => $res[3]["current"]["weather"][0]["main"], |
99
|
2 |
|
"Date5" => date("Y-m-d", $res[4]["current"]["dt"]), |
100
|
2 |
|
"CurrentTemp5" => substr($res[4]["current"]["temp"], 0, 5), |
101
|
2 |
|
"CurrentFeelsLike5" => substr($res[4]["current"]["feels_like"], 0, 5), |
102
|
2 |
|
"CurrentWeather5" => $res[4]["current"]["weather"][0]["main"] |
103
|
|
|
]; |
104
|
2 |
|
return [$json]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|