Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikromike's avatar

Timer for sending open api request.

Hello

I need to create timer in Laravel8, in backend side for processing, Api request 3rd party.

They have limited API to 150 request per minutes. I am processing incoming data from cvs file and file can contain 100k rows, but only one column.

-> I am creating chunk for each 1k rows.

$chunks = array_chunk($data, 1000);

  • after that run via foreach.

foreach ($chunks as $key => $chunk){

-> and dispatch them to job queue.

dispatch(new SaveDataJob($chunk, $businessIds));

-> which creates queue job for model PrhList::saveData

-> which pass it back other foreach. -> which include other dispatch for sending request to API endpoint.

My model

    public static function processData($data)
     {
       unset($data[0]);
      $chunks = array_chunk($data, 1000);
     $pathname = 'tmp-data';
     $files = array();

    foreach ($chunks as $key => $chunk){
   $name = "/tmp{$key}.cvs";
   $path = $pathname.$name;
   $businessIds = array();

  dispatch(new SaveDataJob($chunk, $businessIds)); 

} // end of outter foreach loop //

and other function:

public static function saveData($chunk, $businessIds)
{
    foreach ($chunk as $row){

    $data = implode(" ",$row);
    $demitter = '; ' ;
       array_push($businessIds, $data, $demitter );
       
         // Save to DB
      PrhList::updateOrCreate([
        'business_id' => $data,
      ]);

  $businessId = $data;

        dispatch(new SearchByBusinessId($businessId ));

    } // end of inner foreach loop //

Q) I need to count how many request I have sent to API.

I have tried $count = $chunk->count(); but that works only for collection, I am using array.

I am getting error: "Error: Call to a member function count() on int"

Thanks MikroMike

0 likes
2 replies
lukasyelle's avatar

You are probably going to want to approach this a bit differently. Say you hit the processData method more than once, each thread that gets dispatched, regardless of how you count the chunks, will be on its own time and you could very well go over the api limit even if you space out the api calls perfectly for that one job.

You could keep track of the last API request you make in Redis, for example, and then spread out the requests by 0.4 seconds (150 requests in 60 seconds) to ensure that all requests will be buffered by at least that minimum time. This would prevent your application from hitting the limiter in all scenarios.

mikromike's avatar
mikromike
OP
Best Answer
Level 27

I have resolved timeout issue by building multi-able workers in Linux Systemd.

Please or to participate in this conversation.