/** * Scan one or multiple urls via VirusTotal * @param string|array $urls The urls to scan * @return array The information returned by VirusTotal */ public static function scanUrl($urls) { // The api key must be set! $apikey = config("virus-total-api.api_key"); if (!$apikey) { throw new \Exception("Please setup your VirusTotal api key to use the api.", 1); } // Prepare the url scanner $urlscan = new \VirusTotal\Url($apikey); // Try/Catch to get rate limit exceptions thrown by the api wrapper try { $scanResults = $urlscan->getReport($urls); if ($scanResults['response_code'] == 0) { $scanResults = $urlscan->scan($urls); } } catch (\Exception $e) { if ($e instanceof \VirusTotal\Exceptions\RateLimitException) { return ['success' => false, 'error' => 'rate limit exceeded']; } else { throw new \Exception("Please setup a valid VirusTotal api key to use the api.", 1); } } return array_merge(['success' => true], $scanResults); }
/** * Scans url for viruses. * * @param $data * @return array|bool|float|int|string */ private function urlScan($data) { $url_scan = new \VirusTotal\Url(getenv('VIRUSTOTAL_API_KEY')); $url_resp = $url_scan->getReport($data); return $url_resp; }
<?php require_once '../Vendors/autoload.php'; $urls = array('www.google.com', 'www.yahoo.com', 'infysec.com'); $apiKey = 'your_api_key'; $urlInst = new \VirusTotal\Url($apiKey); $scanResult = array(); echo '<table>'; foreach ($urls as $url) { $report = $urlInst->getReport($url); printf('<h1>Url: %s</h1>', $url); // Obviously, VirusTotal API implements rate limit that you can't hit it too hard. You should consider implement long pulling in Javascript if (false === isset($report['scans'])) { printf('Url %s is not ready. Verbose message:%s', $url, $report['verbose_msg']); continue; } foreach ($report['scans'] as $name => $info) { print '<tr>'; printf('<th>%s</th>', $name); printf('<td>%s</td>', $info['detected']); printf('<td>%s</td>', $info['result']); print '</tr>'; } } echo '</table>';
<?php require_once '../Vendors/autoload.php'; require_once 'config.php'; // hacky, should replace with a micro-framework $callback = $_GET['callback']; if (empty($_GET['url'])) { $errMsg = array('verbose_msg' => 'Please provide an url'); echo renderJsonp($callback, $errMsg); return; } $url = $_GET['url']; $apiKey = API_KEY; $urlInst = new \VirusTotal\Url($apiKey); $scanResponse = $urlInst->scan($url); if (isset($scanResponse['resource'])) { $reportResponse = $urlInst->getReport($scanResponse['resource']); echo $callback . '(' . json_encode($reportResponse) . ');'; echo renderJsonp($callback, $reportResponse); return; } echo renderJsonp($callback, $scanResponse); function renderJsonp($callback, $response) { return $callback . '(' . json_encode($response) . ');'; }