How to Geolocate, Get Street Address from an IP Address in PHP
Whether you have security or UX application use-cases in mind, retrieving street address data from IP addresses can significantly benefit your applications.
Thankfully, you can easily copy & paste from the PHP code examples provided below to leverage a free-to-use “Geolocate IP Address to Street Address” API. Calling this API will quickly return the country code, country name, street address, city, region name and zip code associated with any given IP address.
It’s extremely easy to use. We can install the client SDK by running the following command:
composer require cloudmersive/cloudmersive_validate_api_client
And then we can structure our API calls with the following 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->iPAddressGeolocateStreetAddress($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling IPAddressApi->iPAddressGeolocateStreetAddress: ', $e->getMessage(), PHP_EOL;
}
?>
We can now authenticate our requests with a free-tier API key (to get one, register a free account on the Cloudmersive website).
Just like that, we’re all done! Getting useful IP intelligence is just that simple.