Команде sendPhoto требуется аргумент photo
, определенный как InputFile or String
.
Документ API сообщает:
Photo to send. You can either pass a file_id as String to resend a photo
that is already on the Telegram servers, or upload a new photo using
multipart/form-data.
И
InputFile
This object represents the contents of a file to be uploaded. Must be
posted using multipart/form-data in the usual way that files are
uploaded via the browser.
Итак, я попробовал этот метод
$bot_url = "https://api.telegram.org/bot<bot_id>/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id;
$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, array(
"photo" => "@/path/to/image.png",
));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/root/dev/fe_new.png"));
$output = curl_exec($ch);
Кудри выполняются, но Telegram мне отвечает на это:
Error: Bad Request: Wrong persistent file_id specified: contains wrong
characters or have wrong length
Я также пытался заменить @/path...
на file_get_contents
, но в этом случае Telegram дал мне пустой ответ (а curl_error
пуст!).
Как отправить фото в телеграмму с помощью php + curl?