如何限制每秒卷发数 PHP


how to limit number of curls per second php

我想在服务器上运行一个电报机器人。我用 php 创建了它,它使用 curl 向电报服务器发送请求。机器人电报服务器每秒阻止超过 30 个请求,这意味着我最多应该使用 curl 每秒发送 30 个请求。如何将每秒卷曲的数量限制为 30 个?

你可以在PHP中使用microtime函数来精确地测量时间!例如:

$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;
    }
}