How to Validate HTML Files in PHP

Cloudmersive
2 min readApr 1, 2024

--

We can avoid writing lots of new code to validate our HTML files by copying from complementary, ready-to-run PHP code examples.

We’ll simply use the PHP code examples provided below to call a free API that validates HTML documents for us. This will conveniently abstract validation logic away from our machine, and it’ll also return nicely structured information about any errors and warnings identified within invalid HTML documents.

Our first step is to install the client SDK. Let’s run the below command from our command line to install via composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, let’s quickly grab a free Cloudmersive API key to authorize our requests. This will allow us to make up to 800 API calls per month with no additional commitments (if we reach that limit, our total will simply reset the following month).

Lastly on our brief to-do list, let’s copy the below PHP code to call the function. We can supply our API key in the $configvariable and provide our HTML file path in the $input_file variable:

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

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

Now we can rapidly validate HTML files before we use them for any purpose.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet