PHP code example of timefrontiers / php-location

1. Go to this page and download the library: Download timefrontiers/php-location 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/ */

    

timefrontiers / php-location example snippets


use TimeFrontiers\GeoIP\LocationLookup;
use TimeFrontiers\GeoIP\MaxMindService;

$lookup = new LocationLookup(
    MaxMindService::fromDatabase('/run/geoip/GeoLite2-City.mmdb'),
);

$location = $lookup->locate($trustedClientIp);
echo $location->continent;       // Africa
echo $location->continent_code;  // AF
echo $location->country;         // Nigeria
echo $location->country_code;    // NG

use TimeFrontiers\GeoIP\IpApiService;
use TimeFrontiers\GeoIP\LocationLookup;
use TimeFrontiers\Transport\CurlHttpTransport;
use TimeFrontiers\Transport\HttpRequestOptions;

$transport = new CurlHttpTransport(['geo.example.com']);
$provider = new IpApiService(
    transport: $transport,
    endpoint: 'https://geo.example.com/ip-api-json',
    authorization: 'Bearer ' . $_ENV['GEO_GATEWAY_TOKEN'],
    options: new HttpRequestOptions(
        connectTimeoutMilliseconds: 750,
        totalTimeoutMilliseconds: 2500,
        maximumResponseBytes: 32768,
    ),
);

$location = (new LocationLookup($provider))->locate('1.1.1.1');

$legacy = new \TimeFrontiers\Location('8.8.8.8', $provider);
$legacy->refresh();
echo $legacy->country_code;