How to Get IP Intelligence in PHP
The more information we can gather about the IP addresses accessing our web resources, the more secure and compliant our web applications can be.
Conveniently, by calling a free IP Intelligence API, we can retrieve multiple fields of extremely useful information about any IP address using just a few lines of PHP code.
We can retrieve the exact location of the IP address (as much information as is publicly available), and we can reference the IP against a continually updated list of bot threats. We can even tell if the IP is a tor exit node, which indicates clandestine activity (even if it doesn’t necessarily indicate a threat).
To understand the full scope of our API response, we can review the example JSON response object below:
{
"IsBot": true,
"IsTorNode": true,
"IsThreat": true,
"IsEU": true,
"Location": {
"CountryCode": "string",
"CountryName": "string",
"City": "string",
"RegionCode": "string",
"RegionName": "string",
"ZipCode": "string",
"TimezoneStandardName": "string",
"Latitude": 0,
"Longitude": 0
},
"CurrencyCode": "string",
"CurrencyName": "string",
"RegionArea": "string",
"SubregionArea": "string"
}
To make our API calls, we first need to install the PHP client with Composer:
composer require cloudmersive/cloudmersive_validate_api_client
After that, we can use the below code to call our IP intelligence 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 process, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.
try {
$result = $apiInstance->iPAddressIpIntelligence($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling IPAddressApi->iPAddressIpIntelligence: ', $e->getMessage(), PHP_EOL;
}
?>
To authorize our API calls, we’ll need a free Cloudmersive API key (this will give us a limit of 800 API calls per month with zero commitments). We can paste our API key in the ‘YOUR_API_KEY’
placeholder string within the $config
snippet.
Now we can easily retrieve a significant amount of information about any given IP address in a quick, low-code API call.