Отправка фото с сервера (Ubuntu 20.04+Apache2+PHP) telegram ботом
Телеграм бот написан на PHP, отправляет текстовые сообщения, разобрался как отправлять фото, но только если их брать по url.
Но как отправлять фото хранящиеся на самой машине, по соседству с основным файлом скрипта?
<?php
function sendMessage($chatID,$text,$buttons="",$reply=""){
include $path.'var/vars.php';
$markup = "";
if($reply!=""){
$markup.="&reply_to_message_id=".$reply;
}
if($buttons!=""){
$markup.="&reply_markup=".$buttons;
}
file_get_contents($website.
"sendPhoto?chat_id=".$chatID.
"&text=".$text.
"&photo=https://example.com/img/hello.png".
"&parse_mode=HTML".$markup
);
}
?>
Ответы (1 шт):
Автор решения: Denis
→ Ссылка
Переделал следующим образом, как отправить через file_get_contents не нашел:
<?php
function sendMessage($chatID,$text,$image){
include $path.'var/vars.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $website."sendPhoto?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"chat_id" => $chatID,
"caption" => $text,
"photo" => new CURLFile(realpath("imagepath/".$image)),
"parse_mode" => "HTML"
));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("imagepath/".$image));
$output =curl_exec($ch);
curl_close($ch);
}
?>