How to Syntactically Validate a URL in PHP

Cloudmersive
2 min readMar 31, 2020

--

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 default.new 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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet