How to Validate a Domain Name in PHP
2 min readMar 30, 2020
Domain name validation is actually very easy if you know the right trick. This is especially true if that trick is an API that does all the work for you!
We are going to start off with running this command to install our client.
composer require cloudmersive/cloudmersive_validate_api_client
Now we can go ahead and call our function with this code block here:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\DomainApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$domain = "domain_example"; // string | Domain name to check, for example \"cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.try {$result = $apiInstance->domainCheck($domain);print_r($result);} catch (Exception $e) {echo 'Exception when calling DomainApi->domainCheck: ', $e->getMessage(), PHP_EOL;}?>
And that’s really about it. Now you can test it by entering in a domain name to try. The API will contact DNS services and perform a live validation. There are many other functions within this same API client that allow you to do everything from validating names to email addresses.