PHP SDK

Detectant

Detectant PHP SDK

Detectant is a malware scanning API with support for PHP

Scan files for malware from PHP.

Install

This SDK requires PHP 8.3 or later and a PSR-18 HTTP client implementation.

$composer require detectant/sdk guzzlehttp/guzzle

Set your API key in the environment:

$export DETECTANT_API_KEY="your-api-key"

Create a client

1<?php
2
3use Detectant\DetectantClient;
4
5$detectant = new DetectantClient(
6 apiKey: getenv('DETECTANT_API_KEY') ?: null,
7);

By default, requests are sent to https://api.detectant.com.

Scan one file

The simplest PHP option is a file path:

1use Detectant\Requests\CreateScanRequest;
2use Detectant\Utils\File;
3
4$result = $detectant->scan(new CreateScanRequest([
5 'file' => File::createFromFilepath('./invoice.pdf'),
6]));
7
8echo $result->verdict . PHP_EOL;
9print_r($result->detections);

The call completes after the file has been analyzed and returns its scan result.

Supported file inputs

Choose the input that already fits your application:

1use Detectant\Requests\CreateScanRequest;
2use Detectant\Utils\File;
3
4// A path — recommended when the file is on disk
5$file = File::createFromFilepath('./invoice.pdf');
6
7// File contents already in memory
8$file = File::createFromString(
9 content: $fileBytes,
10 filename: 'invoice.pdf',
11 contentType: 'application/pdf',
12);
13
14// Any PSR-7 StreamInterface
15$file = new File(
16 stream: $stream,
17 filename: 'invoice.pdf',
18 contentType: 'application/pdf',
19);
20
21$result = $detectant->scan(new CreateScanRequest([
22 'file' => $file,
23]));

The SDK accepts file paths, in-memory strings, and PSR-7 streams. For in-memory data and streams, provide a filename and content type when they are known.

Scan a batch

Upload between 1 and 20 files. Results are returned in the same order as the inputs.

1use Detectant\Requests\CreateScanBatchRequest;
2use Detectant\Utils\File;
3
4$batch = $detectant->scanBatch(new CreateScanBatchRequest([
5 'files' => [
6 File::createFromFilepath('./invoice.pdf'),
7 File::createFromFilepath('./archive.zip'),
8 ],
9]));
10
11foreach ($batch->results as $item) {
12 if ($item->scan !== null) {
13 echo $item->filename . ': ' . $item->scan->verdict . PHP_EOL;
14 } else {
15 echo $item->filename . ': ' . $item->error . PHP_EOL;
16 }
17}

One file can fail without preventing the other files in the batch from being scanned. Check each item’s scan and error values.

Configuration

Increase the timeout for large files when needed:

1$detectant = new DetectantClient(
2 apiKey: getenv('DETECTANT_API_KEY') ?: null,
3 options: [
4 'timeout' => 120.0,
5 ],
6);

Requests retry transient failures twice by default. Override retries globally or for one request:

1$detectant = new DetectantClient(
2 apiKey: getenv('DETECTANT_API_KEY') ?: null,
3 options: ['maxRetries' => 3],
4);
5
6$result = $detectant->scan($request, options: ['maxRetries' => 0]);

Custom HTTP client

The SDK discovers an installed PSR-18 client automatically. You can also provide one explicitly:

1use GuzzleHttp\Client;
2
3$detectant = new DetectantClient(
4 apiKey: getenv('DETECTANT_API_KEY') ?: null,
5 options: [
6 'client' => new Client(),
7 ],
8);

Exception handling

1use Detectant\Exceptions\DetectantApiException;
2use Detectant\Exceptions\DetectantException;
3
4try {
5 $result = $detectant->scan($request);
6} catch (DetectantApiException $exception) {
7 echo $exception->getCode() . ': ' . $exception->getMessage() . PHP_EOL;
8} catch (DetectantException $exception) {
9 echo $exception->getMessage() . PHP_EOL;
10}