How to geolocate an IP in PHP

Cloudmersive
1 min readAug 2, 2019

--

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:

<?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;
}
?>

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet