LocatorIpgeobase.class.php
1.59 KB
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
<?php
/**
* User: graymur
* Date: 06.11.13
* Time: 16:14
*/
namespace Cpeople\Classes\Geolocation;
class LocatorIpgeobase extends Locator
{
/**
* @return \Cpeople\Classes\Geolocation\Result $result
*/
public function locate()
{
$url = "http://ipgeobase.ru:7020/geo/?ip=$this->ip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type:application/x-www-form-urlencoded');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($ch);
$result = new Result();
try
{
if (curl_error($ch))
{
throw new \Exception('Error in request, ' . __METHOD__);
}
if (!$xml = simplexml_load_string($curlResult))
{
throw new \Exception('Bad response, ' . __METHOD__);
}
if ((string) $xml->ip->country == '')
{
throw new \Exception('Bad XML, ' . __METHOD__);
}
$result->setCity((string) $xml->ip->city);
$result->setCountry((string) $xml->ip->country);
$result->setRegion((string) $xml->ip->region);
$result->setDestrict((string) $xml->ip->district);
$result->setLatitude((string) $xml->ip->lat);
$result->setLongtitude((string) $xml->ip->lng);
}
catch (\Exception $e)
{
$result->setError($e->getMessage());
}
return $result;
}
}