We can minimize the need to write new code for validating our HTML files by leveraging existing, ready-to-use PHP code examples.
By utilizing the PHP code examples provided below, we can call a free API that validates HTML documents for us. This approach abstracts the validation logic from our system and returns well-structured information about any errors and warnings in the HTML documents.
First, we need to install the client SDK. Run the following command from your command line to install it via Composer:
composer require cloudmersive/cloudmersive_document_convert_api_client
Next, obtain a free Cloudmersive API key to authorize our requests. This key allows up to 800 API calls per month with no additional commitments; if we reach the limit, it resets the following month. Finally, copy the PHP code below to call the validation function. Insert your API key in the $config
variable and provide your 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 quickly validate HTML files before using them for any purpose.
Happy Coding 🚀🚀🚀