执行 60 秒后 Webhook 遇到 "Read timeout expired" 错误

encountering "Read timeout expired" error by Webhook after 60 Sec of execution

最近我遇到了电报机器人 api 的问题,这是我以前没有遇到过的...我使用 Webhook 连接方法来捕获机器人请求并使用 PHP 脚本进行响应,有时触发的脚本需要一分多钟才能完成处理。大约一个月前一切正常,电报机器人等待足够长的时间让脚本完全执行,但现在我的连接暂停并且我在执行 60 秒后通过电报 api "Read timeout expired" 收到此 Webhook 错误然后它再次尝试相同的请求,这些请求一直持续到我的服务器因太多打开的条目而超载...我已经尝试过 connection-handling 尽管它似乎没有用,因为我的连接不是浏览器端。我意识到它应该与 Webhook 的设置本身有关,但我无法弄清楚...有什么想法吗?

这里有一些东西可以给你这个数字:

我的代码:

<?php

...running hundreds of thousands of multi-curl requests that take 10 min for example
...or/ sleep(61);
...or/ basically anything that takes more than 60 seconds to run

?>

Telegram 在 运行 上述脚本 60 秒后对我的 Webhook 状态的响应:

{"ok":true,"result":{"url":"https://????.com/??.php","has_custom_certificate":false,"pending_update_count":1,"last_error_date":1499351442,"last_error_message":"Read timeout expired","max_connections":40}}

这是我放在脚本顶部的代码。它响应电报,因此他们停止等待,脚本继续处理。

<?php
    set_time_limit(0);
    ignore_user_abort(true);
    $out =  json_encode([
      'method'=>'sendMessage',
      'chat_id'=>$my_chat_id,
      'text'=> "Starting process..."
      ]);   
    echo $out;
    header('Connection: close');
    header('Content-Length: '.strlen($out));
    header("Content-type:application/json");
    flush();
    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    }

将此代码放在更新变量之后:

<?php
    $update = json_decode(file_get_contents('php://input'));
       if(isset($update->message) || isset($update->edited_message)) {
                if(time()-((@$update->message->date)?:(@$update->edited_message->date)) > 59) {
           /* print json update*/   exit('Update Time Out !');
                }
            }

注意:当然,您也可以获取更新以接收Json并找出已发出的超时请求。

注意:如果您接收源更新的方法与我的不同,请根据您的源进行更改。

注意:如果您知道您的脚本需要超过一分钟的时间,那么这对您不起作用。