可以同时从多个脚本更新和检查的变量

I've run into a bit of a pickle. Usually I can find solutions to my problems by some extensive googling (to the right SO thread), but not this time.

I'm using an API that lets me access their data by cURL, and this API has a request limit of 500 requests per 10 minutes. If this limit is repeatedly exceeded, your API key gets suspended.

My application, written in PHP, frequently makes requests through different pages - sometimes simultaneously. I need a solution that ensures that I'm under the request limit at all times. I need to be able to check and update a variable that resets every 10 minutes, from all scripts - at the same time. Can this even be done? How should I approach this problem?

I'm not asking for sourcecode to a perfect solution, I'd just like some pointers to how this could be solved, or if it can't be solved - what's the alternate approach?

Thank you in advance.

Sounds like memcache is what your looking for. It can hold variables like PHP objects or base types and give them as well an expiration date. Just google for the memcached extensions for PHP and look over here: http://php.net/manual/en/class.memcache.php.

Furhermore, it can be accessed via TCP and has some failover capabilities. AWS is offering a service compatible with the memcache API calling it ElastiCache

If pages are run by different users, only thing i can think of is storing in a table.

Do something like

$datestring = substr(date("Y-m-d H:i"),0, -1);

in this way you have always same string for minutes 2016-11-07 17:00:00 - 2016-11-07 17:09:59.

Then store in a table with this mechanism:

"INSERT INTO table (datestring, count) VALUES ('$datestring', '1') ON DUPLICATE KEY UPDATE count = count + 1";

of course put datestring char(15) as a unique key.

you check your counting with a select FROM.... comparing datestring you just created and you get no rows at all ora a row with a count value.

This is a solution, of course there are many.