Search code examples
phpapicronphp-5.6round-robin

Is there any option to check if API take more than 10 second to call in PHP?


Actually, I am implementing a cron, where I need to call more than 50 APIs. So, I want to implement something like, if an API takes more than 10 seconds to call, I will skip that.

Actually, I am using PHP 5.6. I have tried to implement Round-Robin. I haven't got anything to check if an API takes more than 10 seconds to call.


Solution

  • You can use ReactPHP tools for this purpose. Here examples how to work with timers https://blog.wyrihaximus.net/2015/01/reactphp-timers/

    Example:

    require 'vendor/autoload.php';
    
    $loop = React\EventLoop\Factory::create();
    
    $i = 0;
    $loop->addPeriodicTimer(1, function(React\EventLoop\Timer\Timer $timer) use (&$i, $loop) {
        if ($i == 0) {
           // send request
        } elseif($i < 10){
           // check response
        } else {
            // cancel request and cancel timer
            $loop->cancelTimer($timer);
        }
        ++$i;
    });
    
    $loop->run();