How to fully validate a URL in PHP

Validating your URLs can help shield you from a great deal of inconvenience down the road. For example it can eliminate broken links entirely. Most methods for URL validation only check syntax, which is not always definitive. I am going to show you how to implement full validation, which also queries the server, checks endpoints, and even runs a virus scan. And all of this is going to be accomplished in just a few minutes with the help of an API.

We begin with installing our client with this command for Composer.

composer require cloudmersive/cloudmersive_validate_api_client

After installation, we can call our URL validation function, shown 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);$request = new \Swagger\Client\Model\ValidateUrlRequestFull(); // \Swagger\Client\Model\ValidateUrlRequestFull | Input URL requesttry {$result = $apiInstance->domainUrlFull($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling DomainApi->domainUrlFull: ', $e->getMessage(), PHP_EOL;}?>

OK, we are already done. You now have your URL validation up and running.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store