1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Ip3; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
class Validera3 implements ContainerInjectableInterface |
9
|
|
|
{ |
10
|
|
|
use ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
protected $apiWeatherKey; |
13
|
|
|
|
14
|
2 |
|
public function getWeatherApi(string $key) |
15
|
|
|
{ |
16
|
2 |
|
$this->apiWeatherKey = $key; |
17
|
2 |
|
} |
18
|
|
|
|
19
|
1 |
|
public function getPositionApi(string $key) |
20
|
|
|
{ |
21
|
1 |
|
$this->apiPositionKey = $key; |
|
|
|
|
22
|
1 |
|
} |
23
|
|
|
|
24
|
2 |
|
public function check($ipadress) |
25
|
|
|
{ |
26
|
2 |
|
$message = $this->getWeather($ipadress)["weatherForecast"]["message"]; |
27
|
2 |
|
if ($message === 0) { |
28
|
1 |
|
return true; |
29
|
|
|
} |
30
|
1 |
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function getCurrentIP() |
34
|
|
|
{ |
35
|
1 |
|
$remote = $_SERVER["REMOTE_ADDR"] ?? '127.0.0.1'; |
36
|
1 |
|
$ipadress = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $remote); |
37
|
1 |
|
return $ipadress; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function getPosition($ipAdress) |
41
|
|
|
{ |
42
|
1 |
|
$ch1 = curl_init(); |
43
|
|
|
|
44
|
1 |
|
curl_setopt($ch1, CURLOPT_URL, 'http://api.ipstack.com/'.$ipAdress.'?access_key='.$this->apiPositionKey); |
|
|
|
|
45
|
1 |
|
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); |
46
|
|
|
|
47
|
1 |
|
$multi = curl_multi_init(); |
48
|
1 |
|
$json = curl_exec($ch1); |
|
|
|
|
49
|
|
|
|
50
|
|
|
do { |
51
|
1 |
|
$status = curl_multi_exec($multi, $active); |
52
|
1 |
|
if ($active) { |
53
|
|
|
curl_multi_select($multi); |
54
|
|
|
} |
55
|
1 |
|
} while ($active && $status == CURLM_OK); |
56
|
|
|
|
57
|
1 |
|
curl_multi_remove_handle($multi, $ch1); |
|
|
|
|
58
|
1 |
|
curl_multi_close($multi); |
59
|
|
|
// $curlLink = curl_init('http://api.ipstack.com/'.$ipAdress.'?access_key='.$this->apiPositionKey); |
60
|
|
|
// curl_setopt($curlLink, CURLOPT_RETURNTRANSFER, true); |
61
|
|
|
// $json = curl_exec($curlLink); |
62
|
|
|
// curl_close($curlLink); |
63
|
|
|
|
64
|
1 |
|
$apiResult = json_decode($json, true); |
65
|
1 |
|
$latitude = $apiResult['latitude']; |
66
|
1 |
|
$longitude = $apiResult['longitude']; |
67
|
1 |
|
$country = $apiResult['country_name']; |
68
|
1 |
|
$city = $apiResult['city']; |
69
|
1 |
|
$geoInfo = array($latitude, $longitude, $country, $city); |
70
|
1 |
|
return $geoInfo; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function getWeather($ipAdress) |
74
|
|
|
{ |
75
|
2 |
|
$ch1 = curl_init(); |
76
|
|
|
|
77
|
2 |
|
$ch2 = curl_init(); |
78
|
|
|
|
79
|
2 |
|
curl_setopt($ch1, CURLOPT_URL, 'http://api.openweathermap.org/data/2.5/forecast?q='.$ipAdress.'&appid='.$this->apiWeatherKey); |
|
|
|
|
80
|
2 |
|
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); |
81
|
|
|
|
82
|
2 |
|
curl_setopt($ch2, CURLOPT_URL, 'http://api.openweathermap.org/data/2.5/find?q='.$ipAdress.'&appid='.$this->apiWeatherKey); |
83
|
2 |
|
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); |
84
|
|
|
|
85
|
2 |
|
$multi = curl_multi_init(); |
86
|
|
|
// $json = curl_exec($ch1); |
87
|
2 |
|
curl_multi_add_handle($multi, $ch1); |
|
|
|
|
88
|
2 |
|
curl_multi_add_handle($multi, $ch2); |
89
|
|
|
|
90
|
|
|
do { |
91
|
2 |
|
$status = curl_multi_exec($multi, $active); |
92
|
2 |
|
if ($active) { |
93
|
2 |
|
curl_multi_select($multi); |
94
|
|
|
} |
95
|
2 |
|
} while ($active && $status == CURLM_OK); |
96
|
|
|
|
97
|
2 |
|
$json = curl_multi_getcontent($ch1); |
|
|
|
|
98
|
2 |
|
$json2 = curl_multi_getcontent($ch2); |
99
|
|
|
|
100
|
2 |
|
curl_multi_remove_handle($multi, $ch1); |
|
|
|
|
101
|
2 |
|
curl_multi_remove_handle($multi, $ch2); |
102
|
2 |
|
curl_multi_close($multi); |
103
|
|
|
// $curlLink = curl_init('http://api.openweathermap.org/data/2.5/forecast?q='.$ipAdress.'&appid='.$this->apiWeatherKey); |
104
|
|
|
// curl_setopt($curlLink, CURLOPT_RETURNTRANSFER, true); |
105
|
|
|
// $json = curl_exec($curlLink); |
106
|
|
|
// curl_close($curlLink); |
107
|
|
|
|
108
|
|
|
// $array[] = json_decode($json, true); |
109
|
|
|
// $array[] = json_decode($json2, true); |
110
|
|
|
$json = [ |
111
|
2 |
|
"weatherForecast" => json_decode($json, true), |
112
|
2 |
|
"weatherHistory" => json_decode($json2, true) |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
// $apiResult3 = json_encode(array_merge(json_decode($json, true),json_decode($json2, true))); |
116
|
|
|
// $apiResult = json_decode($apiResult3, true); |
117
|
|
|
|
118
|
|
|
|
119
|
2 |
|
return $json; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|