How to Check an IP Address in PHP
With a few lines of PHP code, we can easily check out an IP address and gather key information about it.
We’ll use the PHP code examples provided below to call a free API that gathers location details associated with an IP address and tries to identify if the address is a bot threat, tor exit node, or any known IP threat.
To install the PHP client using Composer, let’s execute the following command from the command line:
composer require cloudmersive/cloudmersive_validate_api_client
Next, before we get ready to call the function, let’s turn our attention to authorization. We’ll need a free Cloudmersive API key to authorize our API calls (this will allow us a limit of 800 API calls per month with no additional commitments).
Let’s now call the IP lookup function using ready-to-run PHP code examples (and let’s make sure to enclose our IP address in quotes):
<?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;
}
?>
Now we can quickly return information about interesting or potentially threatening IP addresses using minimal PHP code.