How to validate a Word DOCX Document in PHP
1 min readApr 15, 2020
A simple document validation setup can stand between you and some major irritation down the line. With that goal in mind, today’s lesson covers how to set up such a system in PHP. Additionally, we are going to do it in a minimal amount of time, so let’s get straight into it.
First we need to install our API client; simply use the following command.
composer require cloudmersive/cloudmersive_document_convert_api_client
Now call our function for DOCX validation, like so.
<?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\ValidateDocumentApi(// 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);$input_file = "/path/to/file"; // \SplFileObject | Input file to perform the operation on.try {$result = $apiInstance->validateDocumentDocxValidation($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ValidateDocumentApi->validateDocumentDocxValidation: ', $e->getMessage(), PHP_EOL;}?>
Now simply feed your Word documents in as the input and the API will handle the validation side of things. No problem.