Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP2LocationIo provider #1201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Provider/IP2LocationIo/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
33 changes: 33 additions & 0 deletions src/Provider/IP2LocationIo/.github/workflows/provider.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Provider

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
name: PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.0', '8.1', '8.2']
steps:
- uses: actions/checkout@v3
- name: Use PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: curl
- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Install dependencies
run: composer update --prefer-stable --prefer-dist --no-progress
- name: Run test suite
run: composer run-script test-ci
- name: Upload Coverage report
run: |
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
3 changes: 3 additions & 0 deletions src/Provider/IP2LocationIo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Provider/IP2LocationIo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 1.0.0

First release of this library.
118 changes: 118 additions & 0 deletions src/Provider/IP2LocationIo/IP2LocationIo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Provider\IP2LocationIo;

use Geocoder\Collection;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Client\ClientInterface;

/**
* @author IP2Location <support@ip2location.com>
*/
final class IP2LocationIo extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
public const ENDPOINT_URL = 'https://api.ip2location.io/?key=%s&ip=%s';

/**
* @var string
*/
private $apiKey;

/**
* @var string
*/
private $endpointUrl;

Check failure on line 44 in src/Provider/IP2LocationIo/IP2LocationIo.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Geocoder\Provider\IP2LocationIo\IP2LocationIo::$endpointUrl is unused.

/**
* @param ClientInterface $client an HTTP adapter
* @param string $apiKey an API key
*
* @throws \Geocoder\Exception\InvalidArgument
*/
public function __construct(ClientInterface $client, string $apiKey)
{
parent::__construct($client);

$this->apiKey = $apiKey;
}

public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
if ($this->apiKey === null) {
throw new InvalidCredentials('No API Key provided.');
}

if (!filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The IP2LocationIo provider does not support street addresses, only IPv4 or IPv6 addresses.');
}

if ($address === '127.0.0.1') {
return new AddressCollection([$this->getLocationForLocalhost()]);
}

$url = sprintf(self::ENDPOINT_URL, $this->apiKey, $address);

return $this->executeQuery($url);
}

public function reverseQuery(ReverseQuery $query): Collection
{
throw new UnsupportedOperation('The IP2LocationIo provider is not able to do reverse geocoding.');
}

public function getName(): string
{
return 'ip2location_io';
}

private function executeQuery(string $url): AddressCollection
{
$content = $this->getUrlContents($url);
$data = json_decode($content, true);

if (empty($data) || isset($data['error'])) {
return new AddressCollection([]);
}

$timeZone = timezone_name_from_abbr('', (int) substr($data['time_zone'], 0, strpos($data['time_zone'], ':')) * 3600, 0);

if (isset($data['time_zone_info']['olson'])) {
$timeZone = $data['time_zone_info']['olson'];
}

return new AddressCollection([
Address::createFromArray([
'providedBy' => $this->getName(),
'latitude' => $data['latitude'] ?? null,
'longitude' => $data['longitude'] ?? null,
'locality' => $data['city_name'] ?? null,
'postalCode' => $data['zip_code'] ?? null,
'adminLevels' => [['name' => $data['region_name'], 'level' => 1]],
'country' => $data['country_name'] ?? null,
'countryCode' => $data['country_code'] ?? null,
'timezone' => $timeZone,
]),
]);
}
}
21 changes: 21 additions & 0 deletions src/Provider/IP2LocationIo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 — IP2Location <support@ip2location.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions src/Provider/IP2LocationIo/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# IP2LocationIo Geocoder provider
[![Build Status](https://travis-ci.org/geocoder-php/ip2location-io-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/ip2location-io-provider)
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/ip2location-io-provider/v/stable)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
[![Total Downloads](https://poser.pugx.org/geocoder-php/ip2location-io-provider/downloads)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/ip2location-io-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/ip2location-io-provider)
[![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)
[![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)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

This is the IP2LocationIo provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.

### Install

```bash
composer require geocoder-php/ip2location-io-provider
```

### Note

This provider requires IP2Location.io [IP Geolocation Web Service](https://www.ip2location.io) subscription. It offers free & paid solution with high accuracy.

### Contribute

Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +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}}";
Original file line number Diff line number Diff line change
@@ -0,0 +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}}";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:1769:"{"ip":"8.8.8.8","country_code":"US","country_name":"United States of America","region_name":"California","city_name":"Mountain View","latitude":37.405992,"longitude":-122.078515,"zip_code":"94043","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":32,"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}}";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:1735:"{"ip":"74.125.45.100","country_code":"US","country_name":"United States of America","region_name":"Oklahoma","city_name":"Tulsa","latitude":36.15398,"longitude":-95.99278,"zip_code":"74101","time_zone":"-05:00","asn":"15169","as":"Google LLC","isp":"Google LLC","domain":"google.com","net_speed":"T1","idd_code":"1","area_code":"918","weather_station_code":"USOK0537","weather_station_name":"Tulsa","mcc":"-","mnc":"-","mobile_brand":"-","elevation":218,"usage_type":"DCH","address_type":"Unicast","continent":{"name":"North America","code":"NA","hemisphere":["north","west"],"translation":{"lang":null,"value":null}},"district":"Tulsa 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":"Oklahoma","code":"US-OK","translation":{"lang":"","value":""}},"city":{"name":"Tulsa","translation":{"lang":null,"value":null}},"time_zone_info":{"olson":"America/Chicago","current_time":"2023-10-10T02:24:24-05:00","gmt_offset":-18000,"is_dst":true,"sunrise":"07:26","sunset":"18:55"},"geotargeting":{"metro":"671"},"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}}";
Loading
Loading