PHP code example of timur-flush / geo-detector

1. Go to this page and download the library: Download timur-flush/geo-detector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

timur-flush / geo-detector example snippets


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

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

use TimurFlush\GeoDetector\Adapter\MaxMind;

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

use TimurFlush\GeoDetector\Adapter\MaxMindAPI;

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

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

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

/*
 * 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']

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