Malware File Scanner API in PHP

< Back to Guides

PHP is a general-purpose scripting language geared towards web development. Malware file scanner using PHP, we offer three sample code snippets using different method. Methods includes using cURL, HTTP_Request2, or pecl_http.

Instructions

  1. Get a free API to use by signing up at FileScanAPI.com and replace REPLACE_ME with your API key.
  2. Update location value in payload with url location of your file.
  3. Update callback value in payload to your webhook url. We recommend using webhook.site as a callback url during test.

For 2 and 3, you can find this documented at Offical Documentation

 

Using cURL to check for malware in file

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.filescanapi.com/v1/file/async',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "location": "https://filescanapi.com/icon.png",
    "metadata": {
        "filename": "demo.png"
    },
    "callback": "https://webhook.site"
}',
  CURLOPT_HTTPHEADER => array(
    'x-api-key: REPLACE_ME',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

 

Using HTTP_Request2 to check for file malware

<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.filescanapi.com/v1/file/async');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'x-api-key' => 'REPLACE_ME',
  'Content-Type' => 'application/json'
));
$request->setBody('{\n    "location": "https://filescanapi.com/icon.png",\n    "metadata": {\n        "filename": "demo.png"\n    },\n    "callback": "https://webhook.site"\n}');
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

 

Using pecl_http to detect malware in file

<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://api.filescanapi.com/v1/file/async');
$request->setRequestMethod('POST');
$body = new http\Message\Body;
$body->append('{
    "location": "https://filescanapi.com/icon.png",
    "metadata": {
        "filename": "demo.png"
    },
    "callback": "https://webhook.site"
}');
$request->setBody($body);
$request->setOptions(array());
$request->setHeaders(array(
  'x-api-key' => 'REPLACE_ME',
  'Content-Type' => 'application/json'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();