为什么VirusTotal会返回此错误?

时间:2016-02-23 04:21:03

标签: php api curl

我尝试使用VirusTotal的公共API来扫描文件。这是我的代码:

$post_url = 'https://www.virustotal.com/vtapi/v2/file/scan';    

$filemime = exec("file -b --mime-type '$file_to_scan'");

$post = array('apikey' => $virustotal_api_key, 'file' => '@' . realpath($file_to_scan) . ';type=' . $filemime . ';filename=' . $name);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$post_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Expect:'));
$api_reply = curl_exec($ch);
curl_close($ch);

$api_reply_array = json_decode($api_reply, true);

每当我运行此代码时,都会收到以下错误:

[response_code] => 0
[verbose_msg] => Invalid submission format, the uploaded file must travel as a multipart MIME message, please review the documentation

我花了几个小时试图解决这个问题,但它只是不起作用。有人能指出我正确的方向吗?

这是上述代码中print_r的{​​{1}}:

$post

谢谢!

2 个答案:

答案 0 :(得分:3)

在VirusTotal自己(kudos,Karl)的帮助下弄明白了。问题是PHP 5.6如何改变cURL。如果您使用的是PHP 5.6,则需要添加:

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

答案 1 :(得分:0)

要修复的行是

$post = array('apikey' => $virustotal_api_key, 'file' => '@' . realpath($file_to_scan) . ';type=' . $filemime . ';filename=' . $name);

必须更改为:

$cfile = new CURLFile(realpath($file_to_scan),$filemime,$name);
$post = array('apikey' => $virustotal_api_key, 'file' => $cfile);

然后它也适用于PHP 7:)

相关问题