timur-flush / geo-detector
v2.0.1
2020-03-01 01:06 UTC
Requires
- php: ^7.4
- ext-curl: *
- ext-json: *
- ext-psr: *
- ext-zip: *
- netresearch/jsonmapper: ^1.6
- symfony/filesystem: ^2.7 || ^3.0 || ^4.0 || ^5.0
Requires (Dev)
- composer/composer: 1.10.0-RC || 1.10.0
- geoip2/geoip2: ^2.10.0
- mockery/mockery: ^1.3
- php-coveralls/php-coveralls: 2.2.0
- phpunit/phpunit: 9.0.1
- psr/cache: 1.0.1
- psr/simple-cache: 1.0.1
- squizlabs/php_codesniffer: ^3.5
- symfony/cache: 5.0.4
- symfony/process: ^5.0.4
Suggests
- geoip2/geoip2: ^2.0 | To get information about the client's address via MaxMind
This package is auto-updated.
Last update: 2024-10-29 05:59:26 UTC
README
GeoDetector
This library allows to receive geodata of any IP address.
Installation
composer require timur-flush/geo-detector
Available adapters
SypexGeo (Local base)
Usage:
use TimurFlush\GeoDetector\Adapter\Sypex; # With custom database $adapter = new Sypex('/path/to/custom/database'); # With custom mode $adapter = new Sypex(null, Sypex::SXGEO_FILE | Sypex::SXGEO_MEMORY | Sypex::SXGEO_BATCH); # With custom database & custom mode $adapter = new Sypex('/path/to/custom/database', Sypex::SXGEO_FILE | Sypex::SXGEO_MEMORY | Sypex::SXGEO_BATCH); # Default (also see auto-downloading) $adapter = new Sypex();
You can use auto-downloading database. For example: composer.json
"extra": { "TF_Sypex_Link": "http://link.to.sypex.database.archive.zip", "TF_Sypex_PathToDatabase": "path/to/db/in/your/project.dat" }, "scripts": { "post-install-cmd": [ "TimurFlush\\GeoDetector\\Adapter\\Sypex\\Composer::updateDatabase" ], "post-update-cmd": [ "TimurFlush\\GeoDetector\\Adapter\\Sypex\\Composer::updateDatabase" ] }
Please note that:
- The
extra.TF_Sypex_Link
by default ishttps://sypexgeo.net/files/SxGeoCountry.zip
- The
extra.TF_Sypex_PathToDatabase
by default is./Adapter/Sypex/sypex-database.dat
- A default database path is
./Adapter/Sypex/sypex-database.dat
- Depending on the content of the Sypex database, you may not receive some geo information.
SypexGeo (API)
Usage:
use TimurFlush\GeoDetector\Adapter\SypexAPI; # With custom SypexGeo server $adapter = new SypexAPI('custom.server.com'); # With license key $adapter = new SypexAPI(null, 'your-license-key'); # With custom server & license ky $adapter = new SypexAPI('custom.server.com', 'your-license-key'); # Default $adapter = new SypexAPI();
Please note that:
- A server by default is
api.sypexgeo.net
- Without a license you can get only 10k request per month.
MaxMind
Usage:
use TimurFlush\GeoDetector\Adapter\MaxMind; $adapter = new MaxMind('path/to/database.mmdb');
MaxMind (API)
Usage:
use TimurFlush\GeoDetector\Adapter\MaxMindAPI; $accountId = 0; $licenseKey = 'your-license-key'; $adapter = new MaxMindAPI($accountId, $licenseKey);
Providing a geo data information
ProvideAll()
use TimurFlush\GeoDetector\Entity\GeoData; use TimurFlush\GeoDetector\Entity\Country; use TimurFlush\GeoDetector\Entity\Region; use TimurFlush\GeoDetector\Entity\City; /** * Provides all geo data information * @var GeoData $geoData */ $geoData = $adapter->provideAll('8.8.8.8'); $geoData->getClientAddress(); // 8.8.8.8 /** * @var Country $country */ $country = $geoData->getCountry(); $country->getName(); // returns: United States of America $country->getIso(); // returns: US $country->getLatitude(); // returns a latitude if it's exist $country->getLongitude(); // returns a longitude if it's exist $country->getTimeZone(); // returns a timezone if it's exist /** * @var Region $region */ $region = $geoData->getRegion(); $region->getName(); // returns a region name if it's exist $region->getIso(); // returns a region code if it's exist $region->getLatitude(); // returns a latitude if it's exist $region->getLongitude(); // returns a longitude if it's exist $region->getTimeZone(); // returns a timezone if it's exist /** * @var City $city */ $city = $geoData->getCity(); $city->getName(); // returns a region name if it's exist $city->getIso(); // returns a region code if it's exist $city->getLatitude(); // returns a latitude if it's exist $city->getLongitude(); // returns a longitude if it's exist $city->getTimeZone(); // returns a timezone if it's exist $geoData->getTimeZone(); // get timezone in order city, region, country $geoData->getLatitude(); // get latitude in order from city, region, country $geoData->getLongitude(); // get longitude in order from city, region, country $geoData->getTorStatis(); // determine if the address is an output node in the Tor network $geoData->toJson(); // convert object to json GeoData::createFromJson('{...}'); // create from json
ProvideCountry()
/* * The behavior is identical to the provideCountry() method. * The difference is that it will return an array of country names. */ $adapter->provideCountriesBatch(['1.1.1.1', '8.8.8.8']); // ['AU', 'US']
ProvideAllBatch(array $array)
/* * The behavior is identical to the provideAll() method. * The difference is that it will return an array of GeoData objects. */ $adapter->provideAllBatch(['1.1.1.1', '8.8.8.8']); // GeoData[]
Author
Name: Timur F.
Email: flush02@tutanota.com
License
BSD 3-Clause