How to Geolocate IP Addresses in PHP

Cloudmersive
1 min readApr 26, 2023

--

With consistent, accurate locational information derived from IP addresses, you can provide a more personalized experience for each of your application’s users.

You can easily get your hands on geolocational information from any IP address using code provided in this article. The underlying service will quickly retrieve the country code, country name, city, region code, region name, zip code, time zone, and latitude/longitude coordinates associated with your input IP address in a clearly labeled and structured format.

To get started, you can install the client SDK by running the following command:

composer require cloudmersive/cloudmersive_validate_api_client

After that, you can structure your API call using the following ready-to-run PHP code examples:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\IPAddressApi(


new GuzzleHttp\Client(),
$config
);
$value = "value_example"; // string | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.

try {
$result = $apiInstance->iPAddressPost($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling IPAddressApi->iPAddressPost: ', $e->getMessage(), PHP_EOL;
}
?>

To authenticate requests, you’ll just need to supply a free-tier API key in the $config line (you can get one by registering a free account on the Cloudmersive website).

After that, you’re all done — no more code required! Easy.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet