|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Geocoder\Provider\HostIp; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
|
16
|
|
|
use Geocoder\Model\AddressCollection; |
|
17
|
|
|
use function simplexml_load_string; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Oleg Andreyev <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class HostIpXml extends AbstractHostIp |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
const ENDPOINT_URL = 'http://api.hostip.info/get_xml.php?ip=%s&position=true'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function getName(): string |
|
33
|
|
|
{ |
|
34
|
4 |
|
return 'host_ip_xml'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $url |
|
39
|
|
|
* |
|
40
|
|
|
* @return Collection |
|
41
|
|
|
*/ |
|
42
|
2 |
|
protected function executeQuery(string $url): AddressCollection |
|
43
|
|
|
{ |
|
44
|
2 |
|
$content = $this->getUrlContents($url); |
|
45
|
2 |
|
$xml = simplexml_load_string($content); |
|
46
|
|
|
|
|
47
|
2 |
|
$hostIp = $xml->xpath('/HostipLookupResultSet/gml:featureMember/Hostip'); |
|
48
|
2 |
|
if (empty($hostIp[0])) { |
|
49
|
|
|
return new AddressCollection([]); |
|
50
|
|
|
} |
|
51
|
2 |
|
$hostIp = $hostIp[0]; |
|
52
|
|
|
|
|
53
|
2 |
|
$city = (string) $hostIp->xpath('.//gml:name')[0]; |
|
54
|
2 |
|
$countryName = (string) $hostIp->xpath('.//countryName')[0]; |
|
55
|
2 |
|
$countryCode = (string) $hostIp->xpath('.//countryAbbrev')[0]; |
|
56
|
2 |
|
$coords = $hostIp->xpath('.//ipLocation/gml:pointProperty/gml:Point/gml:coordinates'); |
|
57
|
|
|
|
|
58
|
2 |
|
if (empty($coords)) { |
|
59
|
1 |
|
list($lng, $lat) = [null, null]; |
|
60
|
|
|
} else { |
|
61
|
1 |
|
list($lng, $lat) = explode(',', (string) $coords[0], 2); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
return $this->prepareAddressCollection([ |
|
65
|
2 |
|
'lat' => $lat, |
|
66
|
2 |
|
'lng' => $lng, |
|
67
|
2 |
|
'city' => $city, |
|
68
|
2 |
|
'country_name' => $countryName, |
|
69
|
2 |
|
'country_code' => $countryCode, |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|