1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jiad\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Jiad\Modules\GeoTag; |
8
|
|
|
use Jiad\Modules\Curl; |
9
|
|
|
|
10
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
11
|
|
|
// use Anax\Route\Exception\NotFoundException; |
12
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A sample controller to show how a controller class can be implemented. |
16
|
|
|
* The controller will be injected with $di if implementing the interface |
17
|
|
|
* ContainerInjectableInterface, like this sample class does. |
18
|
|
|
* The controller is mounted on a particular route and can then handle all |
19
|
|
|
* requests for that mount point. |
20
|
|
|
* |
21
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
22
|
|
|
*/ |
23
|
|
|
class APIWeatherController implements ContainerInjectableInterface |
24
|
|
|
{ |
25
|
|
|
use ContainerInjectableTrait; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string $db a sample member variable that gets initialised |
32
|
|
|
*/ |
33
|
|
|
private $db = "not active"; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The initialize method is optional and will always be called before the |
39
|
|
|
* target method/action. This is a convienient method where you could |
40
|
|
|
* setup internal properties that are commonly used by several methods. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
3 |
|
public function initialize() : void |
45
|
|
|
{ |
46
|
|
|
// Use to initialise member variables. |
47
|
3 |
|
$this->db = "active"; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This is the index method action, it handles: |
52
|
|
|
* ANY METHOD mountpoint |
53
|
|
|
* ANY METHOD mountpoint/ |
54
|
|
|
* ANY METHOD mountpoint/index |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
1 |
|
public function indexAction() : object |
59
|
|
|
{ |
60
|
1 |
|
$title = "API Weather"; |
61
|
1 |
|
$page = $this->di->get("page"); |
62
|
|
|
|
63
|
|
|
// $client_ip = $this->di->get("request")->getServer("REMOTE_ADDR", "127.0.0.1"); |
64
|
|
|
|
65
|
|
|
$data = [ |
66
|
|
|
// "client_ip" => $client_ip, |
67
|
1 |
|
]; |
68
|
|
|
|
69
|
1 |
|
$page->add("weather/apidoc", $data); |
70
|
|
|
|
71
|
1 |
|
return $page->render([ |
72
|
1 |
|
"title" => $title |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This is the index method action, it handles: |
78
|
|
|
* ANY METHOD mountpoint |
79
|
|
|
* ANY METHOD mountpoint/ |
80
|
|
|
* ANY METHOD mountpoint/index |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public function sAction($ip) : array |
85
|
|
|
{ |
86
|
1 |
|
$geotag = $this->di->get("geotag"); |
87
|
1 |
|
$ipInfo = $geotag->getGeoInfo($ip); |
88
|
|
|
|
89
|
|
|
$searchOptions = [ |
90
|
1 |
|
"lat" => $ipInfo->latitude, |
91
|
1 |
|
"long" => $ipInfo->longitude |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
// $curl = new Curl(); |
95
|
1 |
|
$curl = $this->di->get("curl"); |
96
|
|
|
|
97
|
1 |
|
$weatherInfo = $curl->sCurl($searchOptions); // Single Curl |
98
|
|
|
|
99
|
|
|
|
100
|
1 |
|
return [[$weatherInfo]]; |
|
|
|
|
101
|
|
|
|
102
|
|
|
// $result = [ |
103
|
|
|
// "summary" => $weatherInfo->daily->summary |
104
|
|
|
// ]; |
105
|
|
|
|
106
|
|
|
// foreach($weatherInfo->daily->data as $key=>$value){ |
107
|
|
|
// $result[gmdate("Y-m-d\ H:i:s", $value->time)] = [ |
108
|
|
|
// "summary" => $value->summary, |
109
|
|
|
// "sunrise" => gmdate("H:i:s", $value->sunriseTime), |
110
|
|
|
// "sunset" => gmdate("H:i:s", $value->sunsetTime), |
111
|
|
|
// "temperature_high_celcius" => round(($value->temperatureHigh - 32) * 5/9, 2), |
112
|
|
|
// "temperature_low_celcius" => round(($value->temperatureLow - 32) * 5/9, 2) |
113
|
|
|
// ]; |
114
|
|
|
// } |
115
|
|
|
|
116
|
|
|
// return [[$result]]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* This is the index method action, it handles: |
121
|
|
|
* ANY METHOD mountpoint |
122
|
|
|
* ANY METHOD mountpoint/ |
123
|
|
|
* ANY METHOD mountpoint/index |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
1 |
|
public function mAction($ip) : array |
128
|
|
|
{ |
129
|
1 |
|
$geotag = $this->di->get("geotag"); |
130
|
1 |
|
$ipInfo = $geotag->getGeoInfo($ip); |
131
|
|
|
|
132
|
|
|
$searchOptions = [ |
133
|
1 |
|
"lat" => $ipInfo->latitude, |
134
|
1 |
|
"long" => $ipInfo->longitude |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
// $curl = new Curl(); |
138
|
1 |
|
$curl = $this->di->get("curl"); |
139
|
|
|
|
140
|
1 |
|
$weatherInfo = $curl->mCurl($searchOptions); // Single Curl |
141
|
|
|
|
142
|
1 |
|
return [[$weatherInfo]]; |
|
|
|
|
143
|
|
|
// $result = []; |
144
|
|
|
|
145
|
|
|
// foreach($weatherInfo as $key=>$value){ |
146
|
|
|
// $result[gmdate("Y-m-d\ H:i:s", $value["daily"]["data"][0]["time"])] = [ |
147
|
|
|
// "summary" => $value["daily"]["data"][0]["summary"], |
148
|
|
|
// "sunrise" => gmdate("H:i:s", $value["daily"]["data"][0]["sunriseTime"]), |
149
|
|
|
// "sunset" => gmdate("H:i:s", $value["daily"]["data"][0]["sunsetTime"]), |
150
|
|
|
// "temperature_high_celcius" => round(($value["daily"]["data"][0]["temperatureHigh"] - 32) * 5/9, 2), |
151
|
|
|
// "temperature_low_celcius" => round(($value["daily"]["data"][0]["temperatureLow"] - 32) * 5/9, 2) |
152
|
|
|
// ]; |
153
|
|
|
// } |
154
|
|
|
|
155
|
|
|
// return [[$result]]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|