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.
Try doing it as a multipart post.
$client->post(
'https://api.telegram.org/botMyApiKey/sendPhoto',
array(
'multipart' => array(
array(
'name' => 'chat_id',
'contents' => '1111111'
),
array(
'name' => 'photo',
'contents' => $contents
)
)
)
);
Guzzle documentation reference
For Guzzle 5.3
use GuzzleHttp\Client;
$client = new Client(['defaults' => [
'verify' => false
]]);
$response = $client->post('https://api.telegram.org/bot[token]/sendPhoto', [
'body' => [
'chat_id' => 'xxxxx',
'photo' => fopen(__DIR__ . '/test.jpg', 'r')
]
]);
var_dump($response);
Note: you must pass the file handle to the 'photo' attribute and not the contents of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With