1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chai17\Models; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Chai17\Models; |
8
|
|
|
|
9
|
|
|
class Location implements ContainerInjectableInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
use ContainerInjectableTrait; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public function getLocation($ipNumber) |
17
|
|
|
{ |
18
|
|
|
$location = []; |
19
|
|
|
$api = $this->di->get("ipstack"); |
20
|
|
|
$ipstack = $api["config"]; |
21
|
|
|
$api = $this->di->get("mapquest"); |
22
|
|
|
$mapquest = $api["config"]; |
23
|
|
|
$validateIP = new Models\ValidateIP; |
24
|
|
|
$test = $validateIP->validate($ipNumber); |
25
|
|
|
if ($test[0]) { |
26
|
|
|
$location = $getJson->cUrl($ipstack["url"]. $ipNumber. '?access_key='. $ipstack["key"]); |
|
|
|
|
27
|
|
|
$location = json_decode($location, true); |
28
|
|
|
$latitude = $location["latitude"]; |
29
|
|
|
$longitude = $location["longitude"]; |
30
|
|
|
$location = [$location["city"], $latitude, $longitude]; |
31
|
|
|
} else { |
32
|
|
|
$search = array('å','ä','ö'); |
33
|
|
|
$replace = array('a','a','o'); |
34
|
|
|
$ipNumber = str_replace($search, $replace, $ipNumber); |
35
|
|
|
$location = $getJson->cUrl($mapquest["url"]. $mapquest["key"]. $mapquest["extra"]. $ipNumber); |
36
|
|
|
$location = json_decode($location, true); |
37
|
|
|
$latitude = $location["results"][0]["locations"][0]["latLng"]["lat"]; |
38
|
|
|
$longitude = $location["results"][0]["locations"][0]["latLng"]["lng"]; |
39
|
|
|
$city = $location["results"][0]["locations"][0]["adminArea5"] ?? "Ingen ort"; |
40
|
|
|
$location = [$city, $latitude, $longitude]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $location; |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|