How to Validate a URL’s Syntax in PHP

Cloudmersive
1 min readApr 24, 2023

--

It’s hard to get far with URLs if they aren’t formed correctly. Thankfully, though, you can easily normalize URL syntax with the below code. While our Validate URL Syntax API won’t check endpoint validity, it will ensure your input URL syntax is valid and return a well-formed URL as well as the top level domain name associated with that URL.

To take advantage of this API, you can start by installing running the following command to install the client SDK:

composer require cloudmersive/cloudmersive_validate_api_client

After that, you can copy & paste ready-to-run PHP code 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->domainUrlSyntaxOnly($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DomainApi->domainUrlSyntaxOnly: ', $e->getMessage(), PHP_EOL;
}
?>

To authenticate your requests, provide a free-tier API key in the $config line, and you’re all done. Simple as that!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet