E
E
Edward2016-08-08 00:52:43
PHP
Edward, 2016-08-08 00:52:43

How to transfer a file using multipart in php (photo from Telegram bot)?

Hello. I've been trying all day but it doesn't work. In telegram ( https://telegram.org) I write a bot. And there the API accepts the photo only as "multipart / form-data" and you need to send a photo that
http://i.imgur.com/ea9PB3H.png
wrote to the address:

$url   = "https://api.telegram.org/bot124163913:AAHQ-axveIAcY24uT8ZlYgEW1jOmFqRmXXE/sendPhoto";
$post_fields = array('chat_id'   => $chat_id,
    'photo'     => file_get_contents("http://i.imgur.com/ea9PB3H.png ")
// 'photo'     => "http://i.imgur.com/ea9PB3H.png "
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);

Can you suggest a working solution?
PS. Submit via form
<form action="https://api.telegram.org/bot124163913:AAHQ-axveIAcY24uT8ZlYgEW1jOmFqRmXXE/sendPhoto" method="post" enctype="multipart/form-data">
        <input type="text" name="chat_id" value="182347970">
        <input type="file" name="photo">
        <input type="submit">
    </form>

works great
UPD
function post($url, $parametros, $ch)
{
    curl_setopt ($ch, CURLOPT_URL,$url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $parametros);
    $result= curl_exec($ch);
    return $result;
}
    $ch = curl_init();
    $data = array(
        'chat_id' => '182347970',
        'photo' => 'http://i.imgur.com/ea9PB3H.png '
    );
    $data2 = http_build_query($data);
    echo post ("https://api.telegram.org/bot124163913:AAHQ-axveIAcY24uT8ZlYgEW1jOmFqRmXXE/sendPhoto", $data2, $ch);
also not working
error
{"ok":false,"error_code":400,"description":"Bad Request: Wrong persistent file_id specified: contains wrong characters or has wrong length"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yakov Vylegzhanin, 2016-08-08
@ed_milson

1) Save the image http://i.imgur.com/ea9PB3H.pnglocally, for example, to a folder tmp.
2) Send a local file to the bot.
3) Clean the folder tmpafter sending.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question