I am trying to upload picture using Telegram Bot API using the following code
if(file_exists($_FILES['fileToUpload']['tmp_name'])){
$new = fopen($_FILES['fileToUpload']['tmp_name'], "rb");
$contents = fread($new, $_FILES['fileToUpload']['size']);
fclose($new);
$client = new Client();
$response = $client->post("https://api.telegram.org/botMyApiKey/sendPhoto", [
'body' => ['chat_id' => '11111111', 'photo' => $contents]
]);
var_dump($response);
}else{
echo("No File");
}
I am getting Nginx 502 Bad Gateway
. Am I using the correct method? I have no issues in obtaining getMe
using the API.
P.S I am using Guzzle 5.3.0 for php compatibility.
2
Answers
I finally found a solution. Pasting my solution for others.
I am puzzled as to why this works with this kind of Guzzle approach and not the other one. I suspect Guzzle not setting the correct header type with the first approach.
Try doing it as a multipart post.
Guzzle documentation reference
For Guzzle 5.3
Note: you must pass the file handle to the ‘photo’ attribute and not the contents of the file.