Getting 502 Error When Sending Images via Telegram Bot API

I’m having trouble with my Telegram bot when trying to send photos. Every time I attempt to upload an image, I get a Nginx 502 Bad Gateway error.

Here’s the code I’m using:

if(file_exists($_FILES['uploadedFile']['tmp_name'])){
    $fileHandle = fopen($_FILES['uploadedFile']['tmp_name'], "rb");
    $fileData = fread($fileHandle, $_FILES['uploadedFile']['size']);
    fclose($fileHandle);
    $httpClient = new Client();
    $result = $httpClient->post("https://api.telegram.org/botMY_BOT_TOKEN/sendPhoto", [
        'body' => ['chat_id' => '123456789', 'photo' => $fileData]
    ]);
    var_dump($result);
}else{
    echo("File not found");
}

The weird thing is that basic API calls like getMe work perfectly fine. Is there something wrong with how I’m formatting the photo upload request? I’m using Guzzle version 5.3.0 because of PHP compatibility requirements.

Any ideas what might be causing this 502 error specifically with photo uploads?

I’ve been hitting the same issues. File size usually causes those 502 errors with Telegram uploads. Check your dimensions and compression first - Telegram’s photo limits aren’t documented well. Your Guzzle version might be the problem too. I stopped reading file data manually and switched to CURLFile objects. That fixed my gateway errors completely. Skip fread and create a CURLFile instance instead - pass that directly. The 502 means Telegram’s servers are rejecting your request format, not a network timeout. Also check your bot token permissions. Some bots need extra photo sending privileges enabled through BotFather.

Your request format’s the problem. You’re sending file data directly in the body array, but Telegram’s API needs multipart/form-data for photo uploads. With Guzzle 5.3.0, use ‘multipart’ instead of ‘body’ when sending files. Try this: php $result = $httpClient->post("https://api.telegram.org/botMY_BOT_TOKEN/sendPhoto", [ 'multipart' => [ [ 'name' => 'chat_id', 'contents' => '123456789' ], [ 'name' => 'photo', 'contents' => $fileData, 'filename' => $_FILES['uploadedFile']['name'] ] ] ]); I’ve hit the same 502 errors with Telegram file uploads. Gateway timeouts happen when the server can’t parse your request format properly. Adding the filename parameter helps too.

had the same issue last month with php telegram bots. 502 errors usually mean you’re hitting telegrams file size limits or sending bad requests. check your file size first - telegram caps photos at 20mb. your guzzle syntax looks off for file uploads too. i switched to curl for uploads and it’s been working fine since.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.