timur-flush/sypexgeo

v2.0.1 2020-03-01 01:06 UTC

This package is auto-updated.

Last update: 2024-09-29 05:01:55 UTC


README

Build Status Coverage Status

GeoDetector

此库允许接收任何IP地址的地理数据。

安装

composer require timur-flush/geo-detector

可用的适配器

SypexGeo(本地库)

用法

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();

您可以使用自动下载的数据库。例如: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"
  ]
}

请注意

  1. 默认情况下,extra.TF_Sypex_Linkhttps://sypexgeo.net/files/SxGeoCountry.zip
  2. 默认情况下,extra.TF_Sypex_PathToDatabase./Adapter/Sypex/sypex-database.dat
  3. 默认数据库路径是 ./Adapter/Sypex/sypex-database.dat
  4. 根据Sypex数据库的内容,您可能无法收到一些地理信息。

SypexGeo(API)

用法

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();

请注意

  1. 默认服务器是 api.sypexgeo.net
  2. 没有许可证,您每月只能获得10k次请求。

MaxMind

用法

use TimurFlush\GeoDetector\Adapter\MaxMind;

$adapter = new MaxMind('path/to/database.mmdb');

MaxMind(API)

用法

use TimurFlush\GeoDetector\Adapter\MaxMindAPI;

$accountId = 0;
$licenseKey = 'your-license-key';

$adapter = new MaxMindAPI($accountId, $licenseKey);

提供地理数据信息

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[]

作者

姓名:Timur F.

邮箱: flush02@tutanota.com

许可证

BSD 3-Clause