【问题标题】:how to limit number of curls per second php如何限制每秒卷曲的次数php
【发布时间】:2016-03-05 11:06:33
【问题描述】:

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

【问题讨论】:

  • bot问题很明显!
  • 每个 curl 请求,使用 time(); 以秒为单位获取 unix 时间戳,将时间保存到数据库中,并将 +1 添加到计数字段。不过,您这样做可能会有些滞后。
  • 如果您发布了代码,我可以做出更适合您的答案。
  • 一种方法是使用多卷曲,一次发送所有 30 个请求,然后 sleep(1)
  • 这也可以用 apache 或 nginx 或任何你使用的东西更容易完成:)

标签: php curl telegram-bot


【解决方案1】:

您可以在 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2017-05-19
    • 2018-08-26
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多