I have several Telegram bots working nice for years; I used 2 ways to send requests to Bot API:
The first is:
file_get_contents($url);
The second is:
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60
]);
curl_exec($ch);
Some days ago I noticed that:
file_get_contents()
stopped working completely returning failed to open stream: Connection timed out
every time, but it works nice with requests to other websites;CURLOPT_CONNECTTIMEOUT
to 1
the waiting period reduced to 1 second or so.file_get_contents()
has started working as before with this context
:
file_get_contents($url, false, stream_context_create([
'socket' => [
'bindto' => '0:0'
]
]));
The last_error_message
is always Read timeout expired
. Server reboot does not help. Direct requests to Bot API from a browser works perfectly.
What is happening and how to fix it?
Its probably because Curl tries to Reverse DNS and since it fails it just waiting for the timeout, you can fix it like this:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 )