简体   繁体   中英

how to limit number of curls per second php

I want to run a telegram bot on a server. I created it with php and it sends requests to telegram servers with curl. bot telegram server prevents more than 30 requests per second, this means that I should send 30 requests per second with curl, at maximum. how can I limit number of curl per second to 30 ?

you can use microtime function in PHP to measure time accurately! for example:

$uT = microtime(true);
$c = 0;
while($condition)
{
    # do curl, ... here
    if(++$c>29)
    {
        if( microtime(true) - $uT < 1)
            usleep(1E6*(1-microtime(true)+$uT));
        $uT = microtime(true);
        $c = 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM