MARS
MARS

Reputation: 587

How to limit external API calls from PHP app

I have a PHP app that is overloading an external API with too many calls per minute. I need to limit it to only 20 calls a minute, but I can't seem to figure it out. I have researched the issue and found this and that, but there is a lack of proper documentation and I don't know how it would work. I understand that this is called "rate limiting", but I guess I skipped that in school.

My app is just sending cURL requests in a loop. I have several loops running in the program to gather all this information together. I could just limit the one loop with a timer for 20 per minute, but I have 17 loops running and I have loops within loops. Is it possible to just limit all the cURL requests within my PHP app with a single helper or something and not edit all my code?

Upvotes: 2

Views: 3762

Answers (1)

Dharman
Dharman

Reputation: 33238

There is no way to rate limit PHP functions using any built-in features. You could write some simple wrapper which would call the API only a given amount of times per minute. A crude example would look like this:

function callAPI($api) {
    static $lastRequest;
    $maxRequestsPerMin = 20;
    if (isset($lastRequest)) {
        $delay = 60 / $maxRequestsPerMin; // 60 seconds / $maxRequestsPerMin
        if ((microtime(true) - $lastRequest) < $delay) {
            // Sleep until the delay is reached
            $sleepAmount = ($delay - microtime(true) + $lastRequest) * (1000 ** 2);
            usleep($sleepAmount);
        }
    }
    $lastRequest = microtime(true);

    // Call you API here
}

However, this will only rate-limit this particular script. If you execute another one, then you will start another counter. Alternatively you could store some round robin table either in a flat file or in a database, and check against it every time you want to call the API.

For advanced usages you should look into message queues or ReactPHP. You do not want to hang your server if such functionality would be exposed to the end-users.

Upvotes: 3

Related Questions