Skip to content

Commit e5685e2

Browse files
Add IP2LocationIo provider
1 parent 722fc19 commit e5685e2

16 files changed

+482
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gitattributes export-ignore
2+
.travis.yml export-ignore
3+
phpunit.xml.dist export-ignore
4+
Tests/ export-ignore
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Provider
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
name: PHP ${{ matrix.php-version }}
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php-version: ['8.0', '8.1', '8.2']
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Use PHP ${{ matrix.php-version }}
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
extensions: curl
24+
- name: Validate composer.json and composer.lock
25+
run: composer validate --strict
26+
- name: Install dependencies
27+
run: composer update --prefer-stable --prefer-dist --no-progress
28+
- name: Run test suite
29+
run: composer run-script test-ci
30+
- name: Upload Coverage report
31+
run: |
32+
wget https://scrutinizer-ci.com/ocular.phar
33+
php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## 1.0.0
6+
7+
First release of this library.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\IP2LocationIo;
14+
15+
use Geocoder\Collection;
16+
use Geocoder\Exception\InvalidCredentials;
17+
use Geocoder\Exception\UnsupportedOperation;
18+
use Geocoder\Http\Provider\AbstractHttpProvider;
19+
use Geocoder\Model\Address;
20+
use Geocoder\Model\AddressCollection;
21+
use Geocoder\Provider\Provider;
22+
use Geocoder\Query\GeocodeQuery;
23+
use Geocoder\Query\ReverseQuery;
24+
use Psr\Http\Client\ClientInterface;
25+
26+
/**
27+
* @author IP2Location <[email protected]>
28+
*/
29+
final class IP2LocationIo extends AbstractHttpProvider implements Provider
30+
{
31+
/**
32+
* @var string
33+
*/
34+
public const ENDPOINT_URL = 'https://api.ip2location.io/?key=%s&ip=%s';
35+
36+
/**
37+
* @var string
38+
*/
39+
private $apiKey;
40+
41+
/**
42+
* @var string
43+
*/
44+
private $endpointUrl;
45+
46+
/**
47+
* @param ClientInterface $client an HTTP adapter
48+
* @param string $apiKey an API key
49+
*
50+
* @throws \Geocoder\Exception\InvalidArgument
51+
*/
52+
public function __construct(ClientInterface $client, string $apiKey)
53+
{
54+
parent::__construct($client);
55+
56+
$this->apiKey = $apiKey;
57+
}
58+
59+
public function geocodeQuery(GeocodeQuery $query): Collection
60+
{
61+
$address = $query->getText();
62+
if ($this->apiKey === null) {
63+
throw new InvalidCredentials('No API Key provided.');
64+
}
65+
66+
if (!filter_var($address, FILTER_VALIDATE_IP)) {
67+
throw new UnsupportedOperation('The IP2LocationIo provider does not support street addresses, only IPv4 or IPv6 addresses.');
68+
}
69+
70+
if ($address === '127.0.0.1') {
71+
return new AddressCollection([$this->getLocationForLocalhost()]);
72+
}
73+
74+
$url = sprintf(self::ENDPOINT_URL, $this->apiKey, $address);
75+
76+
return $this->executeQuery($url);
77+
}
78+
79+
public function reverseQuery(ReverseQuery $query): Collection
80+
{
81+
throw new UnsupportedOperation('The IP2LocationIo provider is not able to do reverse geocoding.');
82+
}
83+
84+
public function getName(): string
85+
{
86+
return 'ip2location_io';
87+
}
88+
89+
private function executeQuery(string $url): AddressCollection
90+
{
91+
$content = $this->getUrlContents($url);
92+
$data = json_decode($content, true);
93+
94+
if (empty($data) || isset($data['error'])) {
95+
return new AddressCollection([]);
96+
}
97+
98+
$timeZone = timezone_name_from_abbr('', (int) substr($data['time_zone'], 0, strpos($data['time_zone'], ':')) * 3600, 0);
99+
100+
if (isset($data['time_zone_info']['olson'])) {
101+
$timeZone = $data['time_zone_info']['olson'];
102+
}
103+
104+
return new AddressCollection([
105+
Address::createFromArray([
106+
'providedBy' => $this->getName(),
107+
'latitude' => $data['latitude'] ?? null,
108+
'longitude' => $data['longitude'] ?? null,
109+
'locality' => $data['city_name'] ?? null,
110+
'postalCode' => $data['zip_code'] ?? null,
111+
'adminLevels' => [['name' => $data['region_name'], 'level' => 1]],
112+
'country' => $data['country_name'] ?? null,
113+
'countryCode' => $data['country_code'] ?? null,
114+
'timezone' => $timeZone,
115+
]),
116+
]);
117+
}
118+
}

src/Provider/IP2LocationIo/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 — IP2Location <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# IP2LocationIo Geocoder provider
2+
[![Build Status](https://travis-ci.org/geocoder-php/ip2location-io-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/ip2location-io-provider)
3+
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/ip2location-io-provider/v/stable)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
4+
[![Total Downloads](https://poser.pugx.org/geocoder-php/ip2location-io-provider/downloads)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
5+
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/ip2location-io-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
6+
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/ip2location-io-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/ip2location-io-provider)
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/ip2location-io-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/ip2location-io-provider)
8+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
9+
10+
This is the IP2LocationIo provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
11+
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
12+
13+
### Install
14+
15+
```bash
16+
composer require geocoder-php/ip2location-io-provider
17+
```
18+
19+
### Note
20+
21+
This provider requires IP2Location.io [IP Geolocation Web Service](https://www.ip2location.io) subscription. It offers free & paid solution with high accuracy.
22+
23+
### Contribute
24+
25+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
26+
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).

src/Provider/IP2LocationIo/Tests/.cached_responses/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:1799:"{"ip":"2001:4860:4860:0000:0000:0000:0000:8888","country_code":"US","country_name":"United States of America","region_name":"California","city_name":"Mountain View","latitude":37.38605,"longitude":-122.08385,"zip_code":"94041","time_zone":"-07:00","asn":"15169","as":"Google LLC","isp":"Google LLC","domain":"google.com","net_speed":"T1","idd_code":"1","area_code":"650","weather_station_code":"USCA0746","weather_station_name":"Mountain View","mcc":"-","mnc":"-","mobile_brand":"-","elevation":31,"usage_type":"DCH","address_type":"Anycast","continent":{"name":"North America","code":"NA","hemisphere":["north","west"],"translation":{"lang":null,"value":null}},"district":"Santa Clara County","country":{"name":"United States of America","alpha3_code":"USA","numeric_code":840,"demonym":"Americans","flag":"https://cdn.ip2location.io/assets/img/flags/us.png","capital":"Washington, D.C.","total_area":9826675,"population":331002651,"currency":{"code":"USD","name":"United States Dollar","symbol":"$"},"language":{"code":"EN","name":"English"},"tld":"us","translation":{"lang":null,"value":null}},"region":{"name":"California","code":"US-CA","translation":{"lang":"","value":""}},"city":{"name":"Mountain View","translation":{"lang":null,"value":null}},"time_zone_info":{"olson":"America/Los_Angeles","current_time":"2023-10-10T00:24:23-07:00","gmt_offset":-25200,"is_dst":true,"sunrise":"07:11","sunset":"18:38"},"geotargeting":{"metro":"807"},"ads_category":"IAB19-11","ads_category_name":"Data Centers","is_proxy":false,"proxy":{"last_seen":9,"proxy_type":"DCH","threat":"-","provider":"-","is_vpn":false,"is_tor":false,"is_data_center":true,"is_public_proxy":false,"is_web_proxy":false,"is_web_crawler":false,"is_residential_proxy":false,"is_spammer":false,"is_scanner":false,"is_botnet":false}}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:1739:"{"ip":"83.227.123.8","country_code":"SE","country_name":"Sweden","region_name":"Blekinge lan","city_name":"Karlskrona","latitude":56.16156,"longitude":15.58661,"zip_code":"37193","time_zone":"+02:00","asn":"2119","as":"Telenor Norge AS","isp":"Telenor Sverige AB","domain":"telenor.se","net_speed":"DSL","idd_code":"46","area_code":"0455","weather_station_code":"SWXX0014","weather_station_name":"Karlskrona","mcc":"240","mnc":"06/08","mobile_brand":"Telenor","elevation":16,"usage_type":"ISP/MOB","address_type":"Unicast","continent":{"name":"Europe","code":"EU","hemisphere":["north","east"],"translation":{"lang":null,"value":null}},"district":"Karlskrona Municipality","country":{"name":"Sweden","alpha3_code":"SWE","numeric_code":752,"demonym":"Swedes","flag":"https://cdn.ip2location.io/assets/img/flags/se.png","capital":"Stockholm","total_area":450295,"population":10099265,"currency":{"code":"SEK","name":"Swedish Krona","symbol":"kr"},"language":{"code":"SV","name":"Swedish"},"tld":"se","translation":{"lang":null,"value":null}},"region":{"name":"Blekinge lan","code":"SE-K","translation":{"lang":"","value":""}},"city":{"name":"Karlskrona","translation":{"lang":null,"value":null}},"time_zone_info":{"olson":"Europe/Stockholm","current_time":"2023-10-10T09:24:24+02:00","gmt_offset":7200,"is_dst":true,"sunrise":"07:18","sunset":"18:10"},"geotargeting":{"metro":"-"},"ads_category":"IAB19-18","ads_category_name":"Internet Technology","is_proxy":false,"proxy":{"last_seen":0,"proxy_type":"-","threat":"-","provider":"-","is_vpn":false,"is_tor":false,"is_data_center":false,"is_public_proxy":false,"is_web_proxy":false,"is_web_crawler":false,"is_residential_proxy":false,"is_spammer":false,"is_scanner":false,"is_botnet":false}}";

0 commit comments

Comments
 (0)