geocoder-php/ip-info-db-provider
IP-Info-DB provider for PHP-GeoCoder (Geocoder PHP). Look up IP address geolocation via the ipinfodb.com API and return standardized Geocoder results. Useful for adding country/region/city data to apps from an IP.
Installation
composer require geocoder-php/ip-info-db-provider
Ensure geocoder-php/geocoder is also installed (required dependency).
Basic Usage
use Geocoder\Geocoder;
use Geocoder\Provider\IpInfoDb\IpInfoDbProvider;
$geocoder = new Geocoder();
$geocoder->registerProvider(new IpInfoDbProvider());
$ip = '8.8.8.8';
$result = $geocoder->geocode($ip);
dd($result->getCoordinates()); // Returns [lat, lng]
First Use Case
$ip = request()->ip();
$location = $geocoder->geocode($ip)->first();
$city = $location->getCity();
Middleware for IP-Based Geo-Lookup
namespace App\Http\Middleware;
use Closure;
use Geocoder\Geocoder;
class GeoMiddleware
{
protected $geocoder;
public function __construct(Geocoder $geocoder)
{
$this->geocoder = $geocoder;
}
public function handle($request, Closure $next)
{
$ip = $request->ip();
$location = $this->geocoder->geocode($ip)->first();
$request->merge(['geo' => $location]);
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\GeoMiddleware::class,
];
Caching Results Use Laravel’s cache to avoid repeated API calls for the same IP:
$cacheKey = "ip_geo_{$ip}";
$location = cache()->remember($cacheKey, now()->addHours(1), function () use ($geocoder, $ip) {
return $geocoder->geocode($ip)->first();
});
Bulk IP Lookup For multiple IPs (e.g., in a report), batch requests:
$ips = ['1.1.1.1', '2.2.2.2'];
$results = collect($ips)->map(function ($ip) use ($geocoder) {
return $geocoder->geocode($ip)->first();
});
Laravel Service Provider Bind the provider to the container for dependency injection:
$this->app->bind(IpInfoDbProvider::class, function ($app) {
return new IpInfoDbProvider($app['config']['services.ipinfodb']);
});
Configure in config/services.php:
'ipinfodb' => [
'api_key' => env('IPINFO_DB_API_KEY'),
'database' => env('IPINFO_DB_DATABASE', 'IPINFO_DB_LITE'),
],
Eloquent Observers
Auto-populate city, country, or latitude fields on model creation:
class UserObserver
{
public function creating(User $user)
{
$ip = request()->ip();
$location = $geocoder->geocode($ip)->first();
$user->city = $location->getCity();
$user->country = $location->getCountry();
}
}
Rate Limiting
artisan command).Database Size
IPINFO_DB database (~100MB) may bloat your project. Use IPINFO_DB_LITE (~10MB) for lightweight needs./var/ipinfo_db) and symlink it to storage/.IPv6 Support
MaxMind) for IPv6 IPs.Time Zone Data
overtrue/geoip) if needed.Case Sensitivity
US vs us) may vary. Normalize with strtoupper():
$country = strtoupper($location->getCountry());
Verify Database Path
Ensure the config points to the correct .mmdb file:
$provider = new IpInfoDbProvider([
'database' => storage_path('app/ipinfo_db/IPINFO_DB_LITE.mmdb'),
]);
Throw an exception if the file is missing:
if (!file_exists($databasePath)) {
throw new \RuntimeException("IPInfoDB database not found at {$databasePath}");
}
Check IP Format
Invalid IPs (e.g., 256.0.0.1) will return null. Validate with:
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$location = $geocoder->geocode($ip)->first();
}
Custom Fields Extend the provider to return additional data (e.g., ISP) by parsing the raw database response:
class CustomIpInfoDbProvider extends IpInfoDbProvider
{
public function getIsp($ip)
{
$data = $this->getData($ip);
return $data['traits']['isp'] ?? null;
}
}
Fallback Providers Combine with other providers for redundancy:
$geocoder->registerProvider(new IpInfoDbProvider());
$geocoder->registerProvider(new MaxMindProvider());
$location = $geocoder->geocode($ip)->first(); // Tries both
Artisan Command for Updates Create a command to auto-update the database:
use Symfony\Component\Process\Process;
class UpdateIpInfoDbCommand extends Command
{
protected $signature = 'ipinfo:update';
protected $description = 'Download the latest IPInfoDB database';
public function handle()
{
$process = new Process(['wget', 'https://example.com/ipinfo_db/IPINFO_DB_LITE.mmdb', '-O', storage_path('app/ipinfo_db/IPINFO_DB_LITE.mmdb')]);
$process->run();
$this->info($process->getOutput());
}
}
How can I help you explore Laravel packages today?