How to Validate URLs using APIs in PHP

Cloudmersive
2 min readMay 5, 2024

We can simplify the URL validation process in our PHP applications by leaning on a few low-code URL validation APIs.

Thanks to complementary PHP code examples provided below, we can quickly and easily structure API calls that accomplish the following URL validation actions:

  1. URL domain name validation
  2. URL syntax validation
  3. URL syntax, domain name, and endpoint validation (all at once)

The domain name validation action performs a live lookup of the URL domain to validate its existence, while the syntax validation action simply ensures URLs are formed correctly when they’re entered in a string input field. The comprehensive validation action incorporates the first two actions and goes a step further, checking if the specific URL endpoint is up and running.

We can structure our API call(s) easily in a few quick steps. First, let’s install the PHP client by executing the below command from our command line:

composer require cloudmersive/cloudmersive_validate_api_client

Next, let’s briefly turn our attention to API call authorization. We can call any of the below APIs with a free Cloudmersive API key (this will allow us to make 800 API calls per month, and our total will reset the following month once we reach it).

To validate the URL domain name, we can use the below code:

<?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
);
$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 to validate URL syntax, we can use the following code instead:

<?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->domainUrlSyntaxOnly($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DomainApi->domainUrlSyntaxOnly: ', $e->getMessage(), PHP_EOL;
}
?>

Finally, to perform an in-depth URL validation API call that checks the syntax, domain, and endpoint validity all at once, we can use the following 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\DomainApi(


new GuzzleHttp\Client(),
$config
);
$request = new \Swagger\Client\Model\ValidateUrlRequestFull(); // \Swagger\Client\Model\ValidateUrlRequestFull | Input URL request

try {
$result = $apiInstance->domainUrlFull($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DomainApi->domainUrlFull: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all there is to it! Now we can easily validate URLs in PHP without writing a bunch of code ourselves.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.