Urgent.News

600+ sources. One page. See who else covered it.

Editions

Tech

The Easiest Way to Look Up GeoIP in Laravel

IP location data is useful when your Laravel application needs a sensible regional default, timezone context, analytics enrichment, or an additional risk signal. The integration does not need a large package or a complicated SDK. With IPRout, you can send an IPv4 or IPv6 address to a simple HTTP endpoint and receive GeoIP and network information as JSON. In this tutorial, we will get a seven-day…

In the world of Laravel applications, leveraging GeoIP data can be immensely useful for various purposes such as regional defaults, time zone context, analytics enrichment, and risk signal enhancement. One can achieve this integration without the need for heavy packages or complex SDKs. By utilizing the IPRout service, sending an IPv4 or IPv6 address to a simple HTTP endpoint yields GeoIP and network information in JSON format.

This article delineates a straightforward process of obtaining a seven-day developer key from IPRout, securely configuring it within a Laravel project, and crafting a reusable GeoIP service. The key aspect of this tutorial revolves around creating an endpoint that accepts an IP address, validates the input, queries IPRout for the pertinent IP context, and then returns a JSON response containing fields like country, region, city, timezone, latitude, longitude, autonomous system number (ASN), and network organization.

To begin with, IPRout offers a developer key that's valid for a week, encompassing 1,000 requests, and does not necessitate a login. To acquire it, navigate to the IPRout developer-key page, generate an API key, and safeguard it with utmost confidentiality. It is imperative to note that the developer-key's allowance and terms might alter, hence refer to the official page before proceeding.

Configuring Laravel requires inserting the developer key and API base URL into the project's `.env` file. Ensure the key is kept out of the codebase and configuration layer by utilizing `env()` function calls in application code. Subsequently, extend the `config/services.php` with an entry for IPRout, defining the key and base URL.

Crafting a reusable IPRout service necessitates the creation of a service class, `App\Services/IPRout.php`, which handles the lookup of GeoIP and ASN information. The service encapsulates a `lookup` method that validates the input IP address, retrieves the API key from the `.env` file, constructs the HTTP request using Laravel's built-in HTTP client, and processes the response.

It includes error handling for scenarios such as invalid API keys, invalid IP addresses, request limit surpasses, and internal server errors at IPRout's end.

The API also supports header-based authentication via `X-API-Key`, though employing Laravel's `withToken()` method for bearer authentication is illustrated for its readability. The HTTP request includes a five-second timeout to safeguard against prolonged external requests that could stall application workers. Depending on your application's specific needs, you can adjust the timeout and implement fallback mechanisms.

Finally, a controller is generated to handle incoming requests to the GeoIP endpoint. The controller leverages the service class to fetch and return GeoIP data in JSON format, ensuring a streamlined and maintainable approach to GeoIP integration in Laravel applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Editorial illustration

How to fix the error “Enable JavaScript and cookies to continue”

Cómo solucionar el error “Enable JavaScript and cookies to continue” Este error aparece cuando Cloudflare (u otro proxy inverso de seguridad) detecta que el navegador del usuario no cumple con los requisitos mínimos para acceder al sitio: JavaScript está deshabilitado o las cookies no están permitidas .

Editorial illustration

I Stopped Reading About SEO and Built a Password Generator Instead

For a while, I spent more time reading about SEO than actually doing SEO. Keyword research, domain authority, backlinks, technical SEO, search intent—there was always another guide to read and another tool to try. Eventually, I decided to stop preparing and build a small website from beginning to end.

More from Monday 3 August →