0
我想管理對API的多個調用。我有一個數組數組,並且在調用一個API時使用數組的每個元素,並且迴應響應。該架構雲方式如下:如何在PHP中管理多個API調用?
<?php
ini_set('max_execution_time', 300);//Did as loop takes >30s
require_once 'other/VirusTotalApiV2.php';
$obj= new VirusTotalAPIV2("abc");//Forming object of API
$handle=fopen("foo.txt","r"); // Obtaining the elements
while($read=fgets($handle))//Each line stores an element
{
$counter=0;
$report=$obj->getFileReport($read);//Calling API function
//var_dump($report);
echo "<table>
<tr>
<td>Antivirus</td>
<td>Result</td>
<td>Update</td>
</tr>";
foreach($report->scans as $key=>$value){
echo "<tr>";
echo "<td>".$key."</td>";
echo "<td>".result($value->result)."</td>";
echo "<td>".$value->update."</td>";
echo "</tr>";
$counter++;
}
}
echo $counter;
fclose($handle);
function result($input){
if($input==null)
{
return "No Issue";
}
}
我可以增加此調用的效率,目前,最終的輸出後,10S-15S receieved(一次),併到那時頁面加載保持。 請建議一些更好的出路。
究竟是什麼問題?這不如預期的那樣工作?從代碼的外觀來看,它看起來效率不高。 – David
@David我粘貼了原始代碼。當我執行這段代碼時,它會在10秒內返回結果。直到那個時候,頁面一直在加載。我想擺脫這一點,因爲我想大規模地做到這一點,當將有超過100個元素。 –
這聽起來像是應該在後臺進程中發生的事情,而不是加載頁面時發生的事情。長時間運行的進程不應該阻止用戶界面。 – David