How to Get the Top-Level Domain Name from a URL in PHP
1 min readApr 24, 2023
Using the below code, you can easily retrieve & store the top-level domain name from any URL without having to highlight and delete the initial URL syntax (i.e., https://) syntax by hand.
You can easily structure your API request using the ready-to-run PHP code examples provided below. Start by running the following command to install the client SDK:
composer require cloudmersive/cloudmersive_validate_api_client
Next, copy the below examples to structure your API call:
<?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\DomainApi(
new GuzzleHttp\Client(),
$config
);
$request = new \Swagger\Client\Model\ValidateUrlRequestSyntaxOnly(); // \Swagger\Client\Model\ValidateUrlRequestSyntaxOnly | Input URL information
try {
$result = $apiInstance->domainGetTopLevelDomainFromUrl($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DomainApi->domainGetTopLevelDomainFromUrl: ', $e->getMessage(), PHP_EOL;
}
?>
Now you can configure your API key and URL-field inputs and get top-level domain information instantly. Easy as pie!