Telgram Bot Bad Gateway

I am trying to upload an image using the TelegramBot 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 get it . Am I using the correct method? I have no problem getting getMe using the API. PS I am using Guzzle 5.3.0 for compatibility with php. Nginx 502 Bad Gateway

+4
source share
3 answers

Try this as a multi-page post.

$client->post(
    'https://api.telegram.org/botMyApiKey/sendPhoto', 
    array(
        'multipart' => array(
            array(
                'name'     => 'chat_id',
                'contents' => '1111111'
            ),
            array(
                'name'     => 'photo',
                'contents' => $contents
            )
        )
    )
);

Gtzz documentation link

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 descriptor to the photo attribute, not the contents of the file.

+1
source

I finally found a solution. Insert my solution for others.

move_uploaded_file($_FILES['photo']['tmp_name'], __DIR__."/temp/".$_FILES['photo']['name']); //Important for Form Upload
$client = new Client();
$request = $client->createRequest('POST', 'https://api.telegram.org/botMyApiKey/sendPhoto');
$postBody = $request->getBody();
$postBody->setField('chat_id', '11111111');
$postBody->addFile(new PostFile('photo', fopen(__DIR__."/temp/".$_FILES['photo']['name'], "r") ));
try{
     $response = $client->send($request);
     var_dump($response);
}catch(\Exception $e){
     echo('<br><strong>'.$e->getMessage().'</strong>');
}

, Guzzle, . , Guzzle .

+1

Guzzle 3:

Guzzle POST requests are sent using the application/x-www-form-urlencodedContent-Type Header if POST fields are present but files are not sent to POST. If the files specified in the POST request, the Content-Type header will become . multipart/form-data

The post () method of the client object takes four arguments: URL, optional headers, mail fields, and an array of request parameters. To send files to the POST request, add the @ character to the array (as if you would use the PHP curl_setopt function). Example:

$request = $client->post('http://httpbin.org/post', array(), array(
    'custom_field' => 'my custom value',
    'file_field'   => '@/path/to/file.xml'
));

So, for the Telegram API, this will be:

$request = $client->post('https://api.telegram.org/botMyApiKey/sendPhoto', array(), array(
    'chat_id' => 'xxxx',
    'photo'   => '@/path/to/photo.jpg'
));
0
source

All Articles