How to Compare Two Word DOCX Documents using PHP

Cloudmersive
2 min readFeb 6, 2023

Comparing DOCX documents to highlight differences is a common task in a variety of industries — especially legal and government, but also in the medical, education and publishing fields.

The below API makes it easy to build a DOCX comparison feature into your file processing applications, ensuring the differences between two input documents will be highlighted automatically without requiring a thorough manual review. You can easily take advantage of this API using ready-to-run PHP code examples provided below, and you can make up to 800 free API calls per month with a free-tier API key (register a free account here to get one).

First things first, let’s run this command to install the PHP SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next & last on our list, let’s call the function below, including our API key in the $config line:

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


new GuzzleHttp\Client(),
$config
);
$input_file1 = "/path/to/inputfile"; // \SplFileObject | First input file to perform the operation on.
$input_file2 = "/path/to/inputfile"; // \SplFileObject | Second input file to perform the operation on (more than 2 can be supplied).

try {
$result = $apiInstance->compareDocumentDocx($input_file1, $input_file2);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CompareDocumentApi->compareDocumentDocx: ', $e->getMessage(), PHP_EOL;
}
?>

Now you can easily compare two input DOCX files based on file path. Nice and easy!

--

--

Cloudmersive

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