PHP code example of knops / virustotal-client

1. Go to this page and download the library: Download knops/virustotal-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

knops / virustotal-client example snippets




use Knops\VirustotalClient\VirusTotalClientFactory;

// Create client with Guzzle (easiest)
$client = VirusTotalClientFactory::createWithGuzzle('your-api-key-here');

// Scan a file and wait for results
$result = $client->scanFileAndWait('/path/to/file.pdf');

// Check if file is malicious
if ($result->isMalicious()) {
    echo "⚠️  File is malicious! Detected by {$result->getMaliciousCount()} engines\n";
    
    // Get details about malicious detections
    foreach ($result->getMaliciousResults() as $engine => $detection) {
        echo "- {$engine}: {$detection['result']}\n";
    }
} else {
    echo "✅ File is clean!\n";
}



use Knops\VirustotalClient\VirusTotalClient;
use GuzzleHttp\Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$httpClient = new Client();
$psr17Factory = new Psr17Factory();

$client = new VirusTotalClient(
    'your-api-key-here',
    $httpClient,
    $psr17Factory, // Request factory
    $psr17Factory  // Stream factory
);

// Upload file for scanning
$scanResponse = $client->scanFile('/path/to/suspicious-file.exe');
$analysisId = $scanResponse->getAnalysisId();

// Check analysis status
$analysis = $client->getAnalysis($analysisId);

if ($analysis->isCompleted()) {
    echo "Scan completed!\n";
    echo "Status: " . ($analysis->isClean() ? 'Clean' : 'Threat detected') . "\n";
} else {
    echo "Scan still in progress...\n";
}

$result = $client->scanFile('/path/to/protected.zip', 'password123');

$analysis = $client->getAnalysis($analysisId);

// Get summary information
$summary = $analysis->getSummary();
print_r($summary);

// Get statistics
$stats = $analysis->getStats();
echo "Malicious: {$stats['malicious']}\n";
echo "Suspicious: {$stats['suspicious']}\n";
echo "Clean: {$stats['harmless']}\n";
echo "Undetected: {$stats['undetected']}\n";

// Get file information
echo "File: {$analysis->getFileName()}\n";
echo "Size: {$analysis->getFileSize()} bytes\n";
echo "Type: {$analysis->getFileType()}\n";
echo "SHA256: {$analysis->getFileHash()}\n";

$scanResponse = $client->scanFile('/path/to/file.pdf');
$analysisId = $scanResponse->getAnalysisId();

// Poll every 5 seconds for up to 2 minutes
$maxWait = 120; // seconds
$pollInterval = 5; // seconds

$startTime = time();
while (time() - $startTime < $maxWait) {
    $analysis = $client->getAnalysis($analysisId);
    
    if ($analysis->isCompleted()) {
        echo "Analysis completed!\n";
        break;
    }
    
    echo "Still scanning... ({$analysis->getStatus()})\n";
    sleep($pollInterval);
}

use Knops\VirustotalClient\Exception\VirusTotalException;

try {
    $result = $client->scanFile('/path/to/file.pdf');
} catch (VirusTotalException $e) {
    echo "Error: " . $e->getMessage() . "\n";
    
    // Handle specific error cases
    if (str_contains($e->getMessage(), 'File not found')) {
        echo "The specified file does not exist.\n";
    } elseif (str_contains($e->getMessage(), 'API request failed')) {
        echo "There was an issue with the VirusTotal API.\n";
    }
}