How to Syntactically Validate a URL in PHP

URL validation is a great way to cut down on broken links, among other uses. But how can we implement this without going through all the trouble of coding it out from scratch? Don’t worry, I’m going to show you an easier way, one that won’t eat up much of your time at all.

To begin this method, we will install our API client with this command.

composer require cloudmersive/cloudmersive_validate_api_client

Moving right along, we are next going to call our function for URL validation from said API.

<?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 GuzzleHttp\Client(),$config);$request = new \Swagger\Client\Model\ValidateUrlRequestSyntaxOnly(); // \Swagger\Client\Model\ValidateUrlRequestSyntaxOnly | Input URL informationtry {$result = $apiInstance->domainUrlSyntaxOnly($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling DomainApi->domainUrlSyntaxOnly: ', $e->getMessage(), PHP_EOL;}?>

And now you can just sit back and relax, because your URL validation problems are solved! Seriously, that’s all there is to it.

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