Whether you're personalizing website experiences, tightening up fraud prevention, or adding richer analytics, IP geolocation is a practical and powerful tool to have in your PHP toolkit.
In this post, we’ll walk through how to implement IP geolocation using the IPinfo API — starting with basic HTTP requests and moving into more efficient options like PHP’s cURL library and our official PHP client.
Explore the options for best IP geolocation API.
By the end of this guide, you'll have several working examples and a clear path to integrating accurate IP data into your own projects.
IP geolocation enables developers to determine the general geographic location of a device based on its IP address. Using PHP and an IP geolocation API like IPinfo’s, you can quickly get started mapping IP addresses interacting with your site to location data such as country, region, or city.
There are many valuable uses of IP geolocation. A few include:
If you want to determine where your users are coming from, IP geolocation is a simple and effective way to do it. We’ll outline how to add geolocation in PHP using the IPinfo API — from retrieving the user’s IP address to accessing accurate location data. You’ll learn how to add geolocation in PHP with IPinfo’s valuable IP context.
Before we jump to the PHP code, let's make a simple request using curl and see what happens.
curl is a command-line program that can be used to make requests quickly and can also help debug them. It's available by default on Windows 10, macOS, and most Linux distributions.
(Note: if you're using PowerShell make sure to use curl.exe command instead of curl since the latter is an alias for Invoke-WebRequest PowerShell command.)
We offer unlimited free lookups for country-level geolocation and ASN data, and all of our other pricing tiers grow alongside your IP data needs.
Now let's see a quick example of curl using the IPinfo geolocation API. Here's the response:
$ curl https://api.ipinfo.io/lite/8.8.8.8?token=$TOKEN
{
"ip": "8.8.8.8",
"asn": "AS15169",
"as_name": "Google LLC",
"as_domain": "google.com",
"country_code": "US",
"country": "United States",
"continent_code": "NA",
"continent": "North America"
}
The details in this response are the ones available on the free plan, but you can see the full potential of the API in the responses documentation.
It's also worth taking a look at the headers, both the requests and responses. curl can help with that.
To get the response headers, simply use the -I
or --head
option:
$curl -I https://api.ipinfo.io/lite/8.8.8.8?token=$TOKEN
HTTP/2 200
access-control-allow-origin: *
content-type: application/json; charset=utf-8
content-length: 209
date: Sun, 20 Jul 2025 20:44:14 GMT
via: 1.1 google
strict-transport-security: max-age=2592000; includeSubDomains
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
From the response headers, you can see that the request was successful. Plus, you can view the content type, which is useful when working with APIs.
Enhance user experience and strengthen your security with rich geolocation data.
Next, to get the request headers, you need to use the -v
option (short for --verbose
):
$ curl -v https://api.ipinfo.io/lite/8.8.8.8?token=$TOKEN
* Trying 34.117.59.81:443...
* Connected to api.ipinfo.io (34.117.59.81) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
> Accept: */*
[...]
We aren't going through every detail of the headers here, but it's important to know what is required in the request headers. If you want to learn more about HTTP and its requests in-depth, the RFC7230 is a great place to start.
But in short, here's what is required when making the request:
curl gives a good example of what should be in the request. From the response headers (and the response itself), we can see that it's working as intended.
Now, let's see how to translate this to PHP and get the geolocation data in this programming language.
As we mentioned previously, programming languages can have more than one way to make requests like this. This is the case with PHP. Let's go over some of the ways to make requests using PHP.
The file_get_contents function is the simplest way to make a request with PHP. Basically, to make a request and get the geolocation data from IPinfo API with this function, all you need is to pass the URL as a parameter:
<?php
// Set the API token, you can get one by signing up at <https://ipinfo.io/signup>
$token = "your_access_token";
// Make the request passing the IP and token and store the result in a variable
$geolocation_data = file_get_contents("https://api.ipinfo.io/lite/8.8.8.8?token=$token");
// Print the result as a string
echo "IP geolocation in PHP with file_get_contents()";
echo $geolocation_data;
?>
The result:
{"ip": "8.8.8.8", "asn": "AS15169", "as_name": "Google LLC", "as_domain": "google.com", "country_code": "US", "country": "United States", "continent_code": "NA", "continent": "North America"}
Note that by default the response is formatted as JSON, but file_get_contents reads the content to String. So to handle the data properly, you would need to parse to JSON. This can be done using the json_decode()
function:
<?php
echo "<p>IP geolocation in PHP with file_get_contents (Lite API):</p>";
// Set the IP and token
$ip = "8.8.8.8";
$token = "<token>"; // Replace with your actual token
// Call the IPinfo Lite endpoint
$ipinfo = file_get_contents("https://api.ipinfo.io/lite/" . $ip . "?token=" . $token);
// Decode the JSON response
$json = json_decode($ipinfo);
// Display the relevant properties
echo "IP: " . $json->ip . "<br>";
echo "ASN: " . $json->asn . "<br>";
echo "AS Name: " . $json->as_name . "<br>";
echo "AS Domain: " . $json->as_domain . "<br>";
echo "Country Code: " . $json->country_code . "<br>";
echo "Country: " . $json->country . "<br>";
echo "Continent Code: " . $json->continent_code . "<br>";
echo "Continent: " . $json->continent . "<br>";
?>
Now you can access each property and get their fields individually.
(Note: the file_get_contents() function requires allow_url_fopen
to be true
to work.)
Another way to get the geolocation data in PHP is using its own cURL library. This library allows the use of more options and can offer better performance. However, you may need to install it separately since it's not available in every PHP install.
The PHP's cURL library has many options, but we're going to start with the basic usage. In this case, it is a request from a registered user. So a token is required for the authentication:
<?php
echo "IP geolocation in PHP with PHP's cURL (Lite API):";
echo PHP_EOL;
// Set the API endpoint
$endpoint = "https://api.ipinfo.io/lite/";
// Set the IP address
$ip = "8.8.8.8";
// Set the API token (replace with your actual token)
$token = "your_access_token";
// Initialize curl session
$curl = curl_init($endpoint . $ip . "?token=" . $token);
// Set curl options to return the response as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the session, making the request
$response = curl_exec($curl);
// Close the cURL session
curl_close($curl);
// Convert JSON string into a PHP object
$json = json_decode($response);
// Print some details of the API by accessing object properties
echo "Data for $ip:" . PHP_EOL;
echo "ASN: $json->asn" . PHP_EOL;
echo "AS Name: $json->as_name" . PHP_EOL;
echo "AS Domain: $json->as_domain" . PHP_EOL;
echo "Country Code: $json->country_code" . PHP_EOL;
echo "Country: $json->country" . PHP_EOL;
echo "Continent Code: $json->continent_code" . PHP_EOL;
echo "Continent: $json->continent" . PHP_EOL;
?>
The result:
Data for 8.8.8.8:
ASN: AS15169
AS Name: Google LLC
AS Domain: google.com
Country Code: US
Country: United States
Continent Code: NA
Continent: North America
Alternatively, you could use the official IPinfo PHP Client Library, which does the heavy lifting and makes it even easier to get IP geolocation data in PHP. The library is open source under the Apache license. It uses the guzzle PHP HTTP client under the hood — one of the most popular PHP libraries.
Besides providing the geolocation data (city, region, country, postal code, latitude, and longitude), the library makes it easier to get details like:
Since these libraries are maintained by the IPinfo team, users can rely on IP address location accuracy for their use case.
<?php
// Install the library via Composer: composer require ipinfo/ipinfo
require_once __DIR__ . '/vendor/autoload.php';
use IPinfo\IPinfoLite;
// Get your API token by signing up at https://ipinfo.io/signup
$access_token = "your_access_token"; // Replace with your actual token
// Create a new IPinfoLite client
$client = new IPinfoLite($access_token);
// Declare a variable with the IP
$ip_address = "8.8.8.8";
// Call the getDetails() method from the IPinfoLite object
$details = $client->getDetails($ip_address);
echo "IP geolocation in PHP with IPinfo PHP Client Library (Lite API) for $ip_address:";
echo PHP_EOL;
// Print the array with all details in the response
echo "All details:" . PHP_EOL;
print_r($details->all);
// Print specific details
echo "Some details:" . PHP_EOL;
echo "ASN: " . $details->asn . PHP_EOL;
echo "AS Name: " . $details->as_name . PHP_EOL;
echo "AS Domain: " . $details->as_domain . PHP_EOL;
echo "Country Code: " . $details->country_code . PHP_EOL;
echo "Country: " . $details->country . PHP_EOL;
echo "Continent Code: " . $details->continent_code . PHP_EOL;
echo "Continent: " . $details->continent . PHP_EOL;
?>
The result:
IP geolocation in PHP with IPinfo PHP Client Library (Lite API) for 8.8.8.8:
All details:
Array
(
[ip] => 8.8.8.8
[asn] => AS15169
[as_name] => Google LLC
[as_domain] => google.com
[country_code] => US
[country] => United States
[continent_code] => NA
[continent] => North America
)
Some details:
ASN: AS15169
AS Name: Google LLC
AS Domain: google.com
Country Code: US
Country: United States
Continent Code: NA
Continent: North America
Another benefit of using the IPinfo Client Library for IP geolocation in PHP is to have the option to use caching. With this feature, the values will be cached in memory for a specified time, thus reducing resource usage.
There's also an official library for the Laravel framework. So if you're looking for a way to get IP geolocation data in PHP with Laravel, this can be very helpful.
Implementing IP geolocation for your organization doesn’t have to be complicated. With just a few lines of PHP and the IPinfo API, you can retrieve accurate location and network details for any IP address — and use that data to improve user experience, detect suspicious traffic, or generate more meaningful insights.
Whether you prefer lightweight requests with file_get_contents(), full control with cURL, or ease of use with the IPinfo PHP client library, you have flexible options to fit your development workflow. Ready to go further? Explore our API and start building with reliable, developer-friendly IP data today.
Internet Data Expert