Команда sendPhoto требует аргумента photo
, определяемого как InputFile or String
.Telegram BOT Api: как отправить фотографию с помощью PHP?
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);
Кудри выполняется, но Телеграмма ответить мне это:
Error: Bad Request: Wrong persistent file_id specified: contains wrong
characters or have wrong length
Я также попытался заменить @/path...
на file_get_contents
, но в этом случае Telegram дайте мне пустой ответ (и curl_error
пуст!).
Какой способ отправить фотографию в телеграмму с помощью php + curl?
Работал для меня отлично !! Легко и элегантно! спасибо – azerafati
@CeceXX: почему вы отредактировали мой ответ? – realtebo
@realtebo Я удалил пух. Откат, если вы считаете, что мое редактирование не имеет значения. – Cesare