【问题标题】:Why cURL requests to Telegram Bot API suddenly became very slow?为什么对 Telegram Bot API 的 cURL 请求突然变得很慢?
【发布时间】:2019-12-22 13:21:56
【问题描述】:

我有几个 Telegram 机器人多年来运行良好;我使用 2 种方式向 Bot API 发送请求:

第一个是:

file_get_contents($url);

第二个是:

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 60
]);
curl_exec($ch);

几天前我注意到:

  1. file_get_contents() 停止工作,每次都完全返回 failed to open stream: Connection timed out,但它适用于对其他网站的请求;
  2. cURL 一直在工作,但很慢:在我向机器人发送消息后,我等待 5-8 秒才能收到答复;当我将 CURLOPT_CONNECTTIMEOUT 更改为 1 时,等待时间减少到 1 秒左右。

file_get_contents() 已开始使用此context:

file_get_contents($url, false, stream_context_create([
    'socket' => [
        'bindto' => '0:0'
    ]
]));

last_error_message 始终为Read timeout expired。服务器重新启动没有帮助。从浏览器直接向 Bot API 发出请求非常有效。

发生了什么以及如何解决?

【问题讨论】:

    标签: php curl telegram-bot centos6 php-7.0


    【解决方案1】:

    这可能是因为 Curl 尝试反向 DNS 并且由于它只是等待超时而失败,您可以这样修复它:

    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2018-08-19
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多