1 | <?php |
||
2 | |||
3 | namespace Anax\Controller; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | use Anax\OpenWeather\NameToGeo; |
||
8 | |||
9 | // use Anax\Route\Exception\ForbiddenException; |
||
10 | // use Anax\Route\Exception\NotFoundException; |
||
11 | // use Anax\Route\Exception\InternalErrorException; |
||
12 | |||
13 | /** |
||
14 | * A sample JSON controller to show how a controller class can be implemented. |
||
15 | * The controller will be injected with $di if implementing the interface |
||
16 | * ContainerInjectableInterface, like this sample class does. |
||
17 | * The controller is mounted on a particular route and can then handle all |
||
18 | * requests for that mount point. |
||
19 | */ |
||
20 | class WeatherApiController implements ContainerInjectableInterface |
||
21 | { |
||
22 | use ContainerInjectableTrait; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * This is the index method action, it handles: |
||
27 | * GET METHOD mountpoint |
||
28 | * GET METHOD mountpoint/ |
||
29 | * GET METHOD mountpoint/index |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | 1 | public function indexActionGet() : array |
|
34 | { |
||
35 | $json = [ |
||
36 | 1 | "message" => "Use POST with IP-address or placename in body to validate", |
|
37 | "example" => "POST /weatherApi/ {'ip': '8.8.8.8', 'placename': 'karlskrona'}", |
||
38 | ]; |
||
39 | 1 | $page = $this->di->get("page"); |
|
40 | |||
41 | 1 | $page->add("anax/v2/plain/pre", [ |
|
42 | 1 | "content" => $json, |
|
43 | ]); |
||
44 | 1 | return [$json]; |
|
45 | } |
||
46 | |||
47 | 1 | public function indexActionPost() |
|
48 | { |
||
49 | 1 | $data = $this->di->request->getBody(); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | // To change %3A to ":" because of http_build_query |
||
51 | // var_dump($data); |
||
52 | 1 | $array = explode("&", $data); |
|
53 | 1 | $data = urldecode($array[0]); |
|
54 | 1 | $name = substr($array[1], 10); |
|
55 | 1 | $ipAdd = substr($data, 3); |
|
56 | |||
57 | 1 | if ($name) { |
|
58 | 1 | $geolocaion = new NameToGeo(); |
|
59 | 1 | $ipjson = $geolocaion -> getGeo($name); |
|
60 | // var_dump($ipjson); |
||
61 | 1 | $lat = $ipjson[0] ->lat; |
|
62 | 1 | $lon = $ipjson[0] ->lon; |
|
63 | 1 | $full_add = $ipjson[0]->display_name; |
|
64 | } else { |
||
65 | 1 | $ipgeoweather = $this->di->get("ipgeoweather"); |
|
66 | 1 | $ipjson = $ipgeoweather->getJson($ipAdd); |
|
67 | 1 | $lat = $ipjson["latitude"]; |
|
68 | 1 | $lon = $ipjson["longitude"]; |
|
69 | 1 | $arr =array($ipjson["country_name"], $ipjson["region_name"], $ipjson["city"]); |
|
70 | 1 | $full_add = implode("->", $arr); |
|
71 | } |
||
72 | 1 | $ipArr = array("address"=> $full_add, "latitude"=>$lat, "longitude"=>$lon); |
|
73 | 1 | $fiveDays = []; |
|
74 | 1 | for ($i=1; $i < 6; $i++) { |
|
75 | 1 | $fiveDays[$i] = strtotime("-$i day"); |
|
76 | } |
||
77 | 1 | $openweather = $this->di->get("openweather"); |
|
78 | 1 | $weather = $openweather->getCurl($lat, $lon); |
|
79 | 1 | $history = $openweather->getHistoryMCurl($lat, $lon, $fiveDays); |
|
80 | $json_data = [ |
||
81 | 1 | "content" => $ipArr, |
|
82 | 1 | "forecast" => $weather, |
|
83 | 1 | "history" => $history, |
|
84 | ]; |
||
85 | 1 | return [$json_data]; |
|
86 | } |
||
87 | } |
||
88 |