【发布时间】: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);
几天前我注意到:
-
file_get_contents()停止工作,每次都完全返回failed to open stream: Connection timed out,但它适用于对其他网站的请求; - 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