1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
14
|
|
|
* The controller will be injected with $di if implementing the interface |
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
17
|
|
|
* requests for that mount point. |
18
|
|
|
* |
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
20
|
|
|
*/ |
21
|
|
|
class WeatherController implements ContainerInjectableInterface |
22
|
|
|
{ |
23
|
|
|
use ContainerInjectableTrait; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function apiKey($api = "") |
27
|
|
|
{ |
28
|
|
|
$curl = curl_init(); |
29
|
|
|
curl_setopt_array($curl, array( |
30
|
|
|
CURLOPT_URL => "$api", |
31
|
|
|
CURLOPT_RETURNTRANSFER => true, |
32
|
|
|
CURLOPT_TIMEOUT => 30, |
33
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
34
|
|
|
CURLOPT_CUSTOMREQUEST => "GET", |
35
|
|
|
CURLOPT_HTTPHEADER => array( |
36
|
|
|
"cache-control: no-cache" |
37
|
|
|
), |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
$response = curl_exec($curl); |
41
|
|
|
|
42
|
|
|
curl_close($curl); |
43
|
|
|
|
44
|
|
|
return json_decode($response, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function weatherActionGet() |
48
|
|
|
{ |
49
|
|
|
$page = $this->di->get("page"); |
50
|
|
|
$iptest = $this->di->request->getGet('location', ""); |
|
|
|
|
51
|
|
|
$validate = $this->weatherValidateGet($iptest); |
52
|
|
|
|
53
|
|
|
if ($validate != false) { |
54
|
|
|
$response = $this->apiKey("https://api.darksky.net/forecast/" . $this->di->get('keys')->weather . "/" . $validate["latitude"] . "," . $validate["longitude"] . "?exclude=currently,%20minutely,%20hourly,alerts&lang=sv&units=si"); |
55
|
|
|
} else { |
56
|
|
|
$response = false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$page->add("ip/weather", [ |
60
|
|
|
"response" => $response |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
return $page->render(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function weatherValidateGet($iptest) |
67
|
|
|
{ |
68
|
|
|
$validated = false; |
69
|
|
|
|
70
|
|
|
if (filter_var($iptest, FILTER_VALIDATE_IP)) { |
71
|
|
|
$validated = $this->APIKey("http://api.ipstack.com/$iptest?access_key=" . $this->di->get("keys")->ip); |
72
|
|
|
} |
73
|
|
|
return $validated; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function weatherRestApiActionGet($iptest = "161.185.160.93") |
77
|
|
|
{ |
78
|
|
|
$validate = $this->weatherValidateGet($iptest); |
79
|
|
|
|
80
|
|
|
if ($validate != false) { |
81
|
|
|
$response = $this->apiKey("https://api.darksky.net/forecast/" . $this->di->get('keys')->weather . "/" . $validate["latitude"] . "," . $validate["longitude"]); |
82
|
|
|
} else { |
83
|
|
|
$response = ["Ip Could not been found!"]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return json_encode($response, JSON_PRETTY_PRINT); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|