Queuing Guzzle Requests With Limits

There's not enough information to really dig deep into this, but to get you started, good APIs typically return a 429 response code when you're exceeding their throttled limit.

You could use $res->getStatusCode() from guzzle to check for this and flash a message back to the user if they're making too many requests too quickly.

Can you give some more information about what your app is doing? Are you making requests in a foreach loop? Is the view dependent on data from this API?


  1. Wrap your API calls with Jobs and push them to separate queue:

    ApiJob::dispatch()->onQueue('api');
    
  2. Use mxl/laravel-queue-rate-limit package (I'm the author) to rate limit api queue. Add this to config/queue.php:

    'rateLimit' => [
        'api' => [
            'allows' => 40,
            'every' => 10
        ]
    ]
    
  3. Run queue worker:

    $ php artisan queue:work --queue api
    

See also this answer.