bytes4sale/iplocator
Laravel package to look up IP address details like location, currency, and language via supported providers (IPDATA, IPSTACK, IP-API). Configure API source and keys in .env, then fetch enriched IP info quickly in your app.
Installation
composer require bytes4sale/iplocator
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
Bytes4Sale\Iplocator\IplocatorServiceProvider::class,
],
Basic Usage Fetch IP details via the facade:
use Bytes4Sale\Iplocator\Facades\Iplocator;
$ipDetails = Iplocator::get('8.8.8.8');
dd($ipDetails);
Expected output:
{
"ip": "8.8.8.8",
"country": "United States",
"region": "California",
"city": "Mountain View",
"zip": "94043",
"latitude": 37.4056,
"longitude": -122.0775,
"isp": "Google LLC",
"org": "Google Cloud Platform"
}
Configuration
Check config/iplocator.php for API key setup (default: ip-api.com free tier).
'api_key' => env('IPLOCATOR_API_KEY'),
'provider' => env('IPLOCATOR_PROVIDER', 'ip-api'),
User IP Detection
$userIp = request()->ip();
$location = Iplocator::get($userIp);
Store in session for multi-request consistency:
session(['user_location' => $location]);
Geotargeted Content
$country = Iplocator::get(request()->ip())->country;
return view('welcome')->with('country', $country);
Bulk IP Lookup
$ips = ['1.1.1.1', '8.8.8.8'];
$results = Iplocator::batch($ips);
// Returns array of IP detail objects
Middleware for Location-Based Logic
namespace App\Http\Middleware;
use Bytes4Sale\Iplocator\Facades\Iplocator;
class CheckLocation
{
public function handle($request, Closure $next)
{
$location = Iplocator::get($request->ip());
if ($location->country !== 'United States') {
abort(403, 'Access denied');
}
return $next($request);
}
}
$location = Cache::remember("ip_{$ip}", now()->addHours(24), function() use ($ip) {
return Iplocator::get($ip);
});
config/iplocator.php:
'providers' => [
'ip-api' => [
'url' => 'http://ip-api.com/json/{ip}',
'key' => null,
],
'ipinfo' => [
'url' => 'https://ipinfo.io/{ip}/json',
'key' => env('IPINFO_TOKEN'),
],
],
Then use:
Iplocator::setProvider('ipinfo')->get($ip);
Rate Limiting
ip-api.com allows 45 requests/minute from an IP.try {
$location = Iplocator::get($ip);
} catch (\Bytes4Sale\Iplocator\Exceptions\RateLimitException $e) {
sleep(60); // Wait 1 minute
retry();
}
IPv6 Support
ip-api.com, which does not fully support IPv6.ipinfo.io (configured as a fallback provider) for IPv6:
Iplocator::setProvider('ipinfo')->get('2001:0db8:85a3::8a2e:0370:7334');
Inconsistent Data
null for fields like zip or org.$location = Iplocator::get($ip);
$city = $location->city ?? 'Unknown';
API Key Leaks
config/iplocator.php exposes them..env and restrict file permissions:
chmod 600 .env
Enable Logging
Set debug to true in config/iplocator.php to log API responses:
'debug' => env('APP_DEBUG', false),
Check logs at storage/logs/laravel.log.
Mocking for Tests
Use the mock() method to simulate API responses:
Iplocator::mock([
'ip' => '8.8.8.8',
'country' => 'Testland',
]);
$location = Iplocator::get('8.8.8.8');
$this->assertEquals('Testland', $location->country);
Custom Fields
Extend the Location model by publishing config:
php artisan vendor:publish --provider="Bytes4Sale\Iplocator\IplocatorServiceProvider" --tag="config"
Add fields to config/iplocator.php under mapping:
'mapping' => [
'ip' => 'ip',
'country' => 'country',
'custom_field' => 'raw.data.custom_key', // Access nested JSON
],
Provider Extensions
Create a custom provider by implementing Bytes4Sale\Iplocator\Contracts\Provider:
namespace App\Providers;
use Bytes4Sale\Iplocator\Contracts\Provider;
class CustomProvider implements Provider
{
public function get($ip)
{
$response = Http::get("https://custom-api.com/{$ip}");
return $response->json();
}
}
Register it in config/iplocator.php:
'providers' => [
'custom' => App\Providers\CustomProvider::class,
],
Event Listeners
Listen for ip.located events to log or process locations:
namespace App\Listeners;
use Bytes4Sale\Iplocator\Events\IpLocated;
class LogLocation
{
public function handle(IpLocated $event)
{
\Log::info('IP located', $event->location->toArray());
}
}
Register in EventServiceProvider:
protected $listen = [
\Bytes4Sale\Iplocator\Events\IpLocated::class => [
\App\Listeners\LogLocation::class,
],
];
How can I help you explore Laravel packages today?