forked from JiSoft/yii2-sypexgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sypexgeo.php
149 lines (138 loc) · 3.79 KB
/
Sypexgeo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Class Sypexgeo.
*
* Provide GeoIP through SypexGeo API
* @link http://sypexgeo.net/
*
* @author Igor Cherkashin aka JiSoft <[email protected]
* @created 11.06.14 18:14
* @version 0.1.0
* @since 2.0
*/
namespace jisoft\sypexgeo;
class Sypexgeo
{
/**
* @var \SxGeo instance.
*/
private $_sypex = null;
/**
* @var string $ip remote client IP-address
*/
public $ip = '';
/**
* @var int $ipAsLong remote client IP-address as integer value
*/
public $ipAsLong = 0;
/**
* @var array $city geo information about city
*/
public $city = [];
/**
* @var array $region geo information about region
*/
public $region = [];
/**
* @var array $country geo information about country
*/
public $country = [];
/**
* Get full geo info by remote IP-address
* @param string $ip source ip, if empty then determine
* @return array geo info|false if error
* result array example:
* ```php
* [
* 'city' => [
* 'id' => 709717,
* 'lat' => 48.023000000000003,
* 'lon' => 37.802239999999998,
* 'name_ru' => 'Донецк',
* 'name_en' => 'Donets\'k',
* 'okato' => '14101',
* ],
* 'region' => [
* 'id' => 709716,
* 'lat' => 48,
* 'lon' => 37.5,
* 'name_ru' => 'Донецкая область',
* 'name_en' => 'Donets\'ka Oblast\'',
* 'iso' => 'UA-14',
* 'timezone' => 'Europe/Zaporozhye',
* 'okato' => '14',
* ],
* 'country' => [
* 'id' => 222,
* 'iso' => 'UA',
* 'continent' => 'EU',
* 'lat' => 49,
* 'lon' => 32,
* 'name_ru' => 'Украина',
* 'name_en' => 'Ukraine',
* 'timezone' => 'Europe/Kiev',
* ],
* ]
* ```
*/
public function get($ip='')
{
if (empty($ip))
$this->getIP();
else if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return false;
} else {
$this->ip = $ip;
$this->ipAsLong = sprintf('%u', ip2long($ip));
}
$this->getSypexGeo();
$data = $this->_sypex->getCityFull($this->ip);
if (isset($data['city']))
$this->city = $data['city'];
if (isset($data['region']))
$this->region = $data['region'];
if (isset($data['country']))
$this->country = $data['country'];
return empty($data) ? false : $data;
}
/**
* Detect client IP address
* @return string IP
*/
public function getIP()
{
if(getenv('HTTP_CLIENT_IP'))
$ip = getenv('HTTP_CLIENT_IP');
elseif(getenv('HTTP_X_FORWARDED_FOR'))
$ip = getenv('HTTP_X_FORWARDED_FOR');
elseif(getenv('HTTP_X_FORWARDED'))
$ip = getenv('HTTP_X_FORWARDED');
elseif(getenv('HTTP_FORWARDED_FOR'))
$ip = getenv('HTTP_FORWARDED_FOR');
elseif(getenv('HTTP_FORWARDED'))
$ip = getenv('HTTP_FORWARDED');
else
$ip = getenv('REMOTE_ADDR');
$this->ip = $ip;
$this->ipAsLong = sprintf('%u', ip2long($ip));
return $ip;
}
/**
* @return \SxGeo instance.
*/
public function getSypexGeo()
{
if (!is_object($this->_sypex)) {
$this->_sypex = $this->createSxGeo();
}
return $this->_sypex;
}
/**
* Creates SxGeo instance.
* @return \SxGeo instance.
*/
protected function createSxGeo()
{
return new \jisoft\sypexgeo\SxGeo();
}
}