How to geolocate an IP in PHP

Here is a very quick rundown of how to use one of our APIs to geolocate an IP address. Let’s put the following reference in our library:

"require": {
"cloudmersive/cloudmersive_validate_api_client": "^1.4",

Then activate the iPAddressPost function:

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(),
$value = "value_example"; // string | IP address to geolocate, e.g. \"\". The input is a string so be sure to enclose it in double-quotes.
try {
$result = $apiInstance->iPAddressPost($value);
} catch (Exception $e) {
echo 'Exception when calling IPAddressApi->iPAddressPost: ', $e->getMessage(), PHP_EOL;

Here is an example of what the return will look like:

"CountryCode": "US",
"CountryName": "United States",
"City": "Everett",
"RegionCode": "WA",
"RegionName": "Washington",
"ZipCode": "98201",
"TimezoneStandardName": "America/Los Angeles",
"Latitude": 47.9884,
"Longitude": -122.2006

Note that besides the location we are also given the country, region, city, zip code, and time zone.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store