Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

spatie/spatie.be

493 stars
1 code files
View spatie/spatie.be on GitHub

app/Support/FreeGeoIp/FreeGeoIp.php

Open in GitHub
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
 
class FreeGeoIp
{
public static function getCountryCodeForIp(string $ip): string
{
return Cache::remember("countryCodeIp{$ip}", 60 * 5, function () use ($ip) {
$response = Http::get("https://freegeoip.app/json/{$ip}");
 
if ($response->status() !== 200) {
return 'US';
}
 
$countryCode = $response->json('country_code', 'US');
 
if (empty($countryCode)) {
return 'US';
}
 
return $countryCode;
});
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.