Josh Ferrara
Josh Ferrara

Reputation: 767

Get Value of PHP Array

I'm attempting to use the VirusTotal API to return the virus scan for a certain file. I've been able to get my current PHP code to upload the file to VirusTotal as well as get the results in an array. My question is, how would I get the [detected] value from every virus scanner under the scans object? My PHP code is below as well as a link to the output of the array.

require_once('VirusTotalApiV2.php');

/* Initialize the VirusTotalApi class. */
$api = new VirusTotalAPIV2('');

if (!isset($_GET["hash"])) {
    $result = $api->scanFile('file.exe');
    $scanId = $api->getScanID($result);
    $api->displayResult($result);
} else {
    $report = $api->getFileReport($_GET["hash"]);
    $api->displayResult($report);
    print($api->getSubmissionDate($report) . '<br>');
    print($api->getReportPermalink($report, TRUE) . '<br>');
}

http://joshua-ferrara.com/viruscan/VirusTotalApiV2Test.php?hash=46faf763525b75b408c927866923f4ac82a953d67efe80173848921609dc7a44

Upvotes: 0

Views: 338

Answers (3)

VinceOmega
VinceOmega

Reputation: 105

You would probably have to iterate each object under scans in a for loop and either store them in yet another array or echo them out of just want to print. For example

$detectedA = {nProtect, CAT-QuickHeal, McAfee...nth};
$datContainer = array();

for ($i = 0; i < $api.length ; i++){

   //Either store in an array
   $api->$scans->detectedA(i)-> detected = $datContainer(i);

   //Or echo it all
   echo $api->$scans->detectedA(i)->detected;

   return true;

}

Granted that's probably not the way you access that object but the idea still applies.

Upvotes: 2

DanRedux
DanRedux

Reputation: 9359

Store the scans into an array (of scans), then just loop through the array as usual.

foreach($scans as $scan) echo $scan->detected;

Or, if I'm not quite understanding the question right, is detected an array (or an object)?

  • Edit because of your comments -

The object returned holds an object of objects, so you need to do some casting.

foreach((array)$scans as $scanObj) {
    $scan=(array)$scanObj;
    foreach($scan as $anti) {
        print $anti->detected; } }

Upvotes: 0

Peter
Peter

Reputation: 2556

This description of stdClass demonstrates how you can not only store arbitrary tuples of data in an object without defining a class, but also how you can cast an arbitrary object as an array - which would then let you iterate over the sub-objects in your case.

Or, if I've misunderstood your question and you're actually getting an array back from the VirusTotal API and not a stdClass instance, then all you need to do is loop.

Upvotes: 0

Related Questions