0

I want to manage multiple calls to an API. I am having an array of data and each element of array is used while calling an API and response is echoed back. The architecture goes following way:

<?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"; } } 

Can I increase the efficiency of this calling as currently, the final output is receieved(at once) after 10s-15s and till that time page keeps loading. Please suggest some better way out.

5
  • What exactly is the problem? How is this not working as expected? It doesn't seem particularly inefficient from the looks of the code. Commented Jun 18, 2015 at 16:44
  • @David I have pasted the original code. As I execute this code, it returns result in more than 10s. Till that time, the page keeps loading. I want to get rid of this as I want to do it on large scale when there will be more than 100 elements. Commented Jun 18, 2015 at 17:11
  • This sounds like something that should be happening in a background process, not when loading a page. Long-running processes shouldn't block the UI. Commented Jun 18, 2015 at 17:12
  • @David even if I use ajax calls, it will be taking place in background but still it will take 10-15s to return back the data. My concern is to improve effeciency of calling API to get faster results. Commented Jun 18, 2015 at 17:20
  • If the curl_multi suggestion below doesn't do the trick, you might need to use threads: php.net/manual/en/intro.pthreads.php Commented Jun 18, 2015 at 17:23

1 Answer 1

1

Not sure what the HTTP client is in use, but assuming its a wrapper of cURL you could look at curl_multi.

Allows the processing of multiple cURL handles asynchronously.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.