How to virus scan a DOCX Document in PHP
2 min readMar 4, 2020
Nobody wants to deal with the hassle of setting up virus scanning in PHP. That’s OK, though, because now you don’t have to. After this simple API tutorial, you will be able to implement your own virus scanning solution in mere minutes.
We are going to start off by installing our client, which just means running this command from your Composer command line:
composer require cloudmersive/cloudmersive_virusscan_api_client
Once our install has finished, it’s time to call our function:
<?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\ScanApi(// 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->scanFile($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ScanApi->scanFile: ', $e->getMessage(), PHP_EOL;}?>
And with that, we are done. So easy! All of the hard work will be tackled by the API, which will then provide us with a diagnosis and list of possible threats.