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

ufodisko's avatar

How to send an API request every X seconds for a specific period of time?

I would like to be able to create a small system that sends an API request every X seconds for a period of time I specify (10mins, 30mins, 60mins...) and then manipulate the response that comes in. (submit to db...)

What do I need to know to start doing such a thing?

Cron jobs? Queues? Observers?

Any tutorials or code samples are much appreciated.

0 likes
6 replies
LaryAI's avatar
Level 58

To achieve this, you can use a combination of cron jobs and queues. Here's how you can do it:

  1. Create a Laravel command that sends the API request and manipulates the response. You can use Guzzle to send the request and manipulate the response.
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use GuzzleHttp\Client;

class SendApiRequest extends Command
{
    protected $signature = 'send-api-request';
    protected $description = 'Send API request and manipulate response';

    public function handle()
    {
        $client = new Client();
        $response = $client->request('GET', 'https://example.com/api');
        $data = json_decode($response->getBody(), true);

        // Manipulate the response here
        // Submit to database, etc.
    }
}
  1. Create a cron job that runs the command every X seconds. You can use the * * * * * syntax to run the command every minute and then use the sleep() function to delay the execution for X seconds.
* * * * * php /path/to/artisan send-api-request && sleep X

Replace X with the number of seconds you want to delay the execution.

  1. To run the cron job for a specific period of time, you can use the timeout command. For example, to run the cron job for 10 minutes, you can use the following command:
timeout 10m crontab -e

This will open the crontab file in the editor and run the cron job for 10 minutes.

  1. Finally, to run the cron job in the background, you can use the nohup command. For example:
nohup timeout 10m crontab -e > /dev/null 2>&1 &

This will run the cron job in the background and redirect the output to /dev/null.

That's it! This should send the API request every X seconds for a specific period of time and manipulate the response that comes in.

Snapey's avatar

How will you start this off?

You could queue a job. In that job start a counter that is the number of iterations wanted. So if you want it to run for 60 minutes at 10 minute intervals, the iterations would be 6.

Then in the job, do what you need with the API and the data, then before the job ends, reduce the iterations count. If not zero, then dispatch another copy of the same job (from within the first job) with a 10 minute delay. Also pass it the number of iterations left. Then let this job end normally.

After 10 minutes, this second job will run, process the data, decrement the iterations and possibly launch another instance of the job, again with a 10 minute delay, again passing along the reduced iterations counter.

Does that make sense?

1 like
ufodisko's avatar

@Snapey

How will you start this off?

I wanted to launch it from within the admin panel I'm working on but after reading Task Scheduling I'm not sure that is possible?

Regarding your suggestion, should I use job queues instead of task scheduling? What is the difference?

Snapey's avatar

@ufodisko scheduling will trigger at a specific time or interval.

What you want seems better suited to my queues suggestion. You only need to dispatch a job and give it the number of iterations. You can do this from a controller.

1 like
ufodisko's avatar

@Snapey I re-read your original comment and honestly it's a bit confusion.

Would a simple for loop for the iteration do the job?

If not, can you please share an example of how one might go about achieving this?

PS: I'm perfectly happy with dispatching the job for X times, without needing to specify the length of time it should run for.

Snapey's avatar

if you did a foreach loop (for an hour) where would this code run?

You could use a foreach loop and create multiple jobs each delayed by an additional 10 minutes.

But its only one solution. It's not the only solution, just a suggestion.

Please or to participate in this conversation.