PHP code example of webmaster / maxmind-db-reader

1. Go to this page and download the library: Download webmaster/maxmind-db-reader 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/ */

    

webmaster / maxmind-db-reader example snippets



namespace App\Service;

use MaxMind\Db\Reader;

class IpLocationService
{
    private $reader;

    public function maxMind(): Reader
    {
        $dbFile = storage_path() . '/ip/GeoIP2-City.mmdb';
        $dbContents = file_get_contents($dbFile, false);
        return new Reader($dbFile, $dbContents);
    }

    public function search(string $ip): string
    {
        if (! $this->reader) {
            $this->reader = $this->maxMind();
        }

        $location = $this->reader->get($ip);

        return $location;
    }

}