I’m encountering an issue in my Laravel application where a POST request to an external API (Telegram Bot API) is failing with the following error:
cURL error 35: Recv failure: Connection reset by peer
I have set up a simple route to trigger a Telegram bot message using Laravel’s Http client:
use IlluminateHttpRequest;
use IlluminateSupportFacadesHttp;
Route::get('bot', function (Request $request) {
$url = 'https://api.telegram.org/bot<my-token>/sendMessage';
Http::connectTimeout(20)->post($url, [
'chat_id' => '<chat-id>',
'text' => 'Got a new request!'
]);
});
When I open the URL manually with the necessary query parameters, it works as expected and sends the message to the Telegram chat. However, when making the request within Laravel, I receive the above cURL error.
What I Have Tried:
- Checked URL and Parameters: Manually accessing the URL with query string parameters works fine, so the API URL is correct.
- SSL Verification: I attempted disabling SSL verification temporarily:
Http::withOptions(['verify' => false])
but I still encountered the same error.
- Setting Specific SSL/TLS Version: I tried enforcing TLS 1.2:
Http::withOptions(['curl' => [CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2]])
I’ve also tried to use Client (GuzzleHttpClient) too. But same error occurs..
Environment Details:
- Laravel Version: 11.2
- Ubuntu Version: 24.04 LTS
- cURL Version: 8.5.0
- PHP Version: 8.3.6
- Nginx Version: 1.24.0
Thanks in advance
Amir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
SOLVED: I just needed to turn on a VPN (because Telegram is unavaiable in my country)
I used Nekoray and then I tried to use it’s proxy on my request. check the below code:
$url = 'https://api.telegram.org/<token>/sendMessage';
Http::withOptions([
'proxy' => '127.0.0.1:2081'
])->connectTimeout(20)->post($url, [
'chat_id' => <chat_id>,
'text' => 'Got a new request!'
]);
Amir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.