Не работает отправка картинки в телеграм
Помогите, почему отправка простого текстового сообщения работает, а отправка изоображения нет?
webhook.php :
<?php
define('TGKEY', 'Токен');
include_once('tg.class.php');
$body = file_get_contents('php://input');
$arr = json_decode($body, true);
$tg = new tg(TGKEY);
$tg_id = $arr['message']['chat']['id'];
$rez_kb = array();
$message_text = $arr['message']['text'];
$tg->sendChatAction($tg_id);
$sms_rev='';
switch($message_text){
case '/start':
$sms_rev = 'Здравствуй! Я - бот с артами, от @theend_arts
Просто пропиши /art и посмотри что выйдет!';
$type ='text';
break;
case '/help':
$sms_rev = 'Я могу выполнить следующюю функцию:
/rev - переворачиваею строку наоборот.
';
$type ='text';
break;
case '/rev':
$sms_rev = strrev($message_text);
$type ='text';
break;
case '/randomart':
$sms_rev = '111';
$image = 'https://theend.pp.ua/all/theendarst_xyz_123.jpg';
$type ='img';
break;
}
if ($type == 'img') {
$tg->tgBotMessage($image, $sms_rev, $tg_id);
} else {
$tg->send($tg_id, $sms_rev, $rez_kb);
}
//$tg->send($tg_id, $sms_rev, $rez_kb);
exit('ok'); // говорим телеге, что все окей
tg.class.php:
<?php
class TG {
public $token = '';
public function __construct($token) {
$this->token = $token;
}
public function send($id, $message, $kb) {
$data = array(
'chat_id' => $id,
'text' => $message,
'parse_mode' => 'HTML',
'disable_web_page_preview'=>true,
'reply_markup' => json_encode(array('inline_keyboard' => $kb))
);
$this->request('sendMessage', $data);
}
public function tgBotMessage($image, $message, $id) {
$url = ('https://api.telegram.org/bot' . TGKEY . '/sendPhoto?chat_id=' . $id);
$post_fields = array('chat_id' => $id,
'caption' => $message,
'photo' => new CURLFile($image),
'reply_markup' => json_encode(array('inline_keyboard' => $kb))
);
$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);*/
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
}
public function editMessageText($id, $m_id, $m_text, $kb=''){
$data=array(
'chat_id' => $id,
'message_id' => $m_id,
'parse_mode' => 'HTML',
'text' => $m_text
);
if($kb)
$data['reply_markup']=json_encode(array('inline_keyboard' => $kb));
$this->request('editMessageText', $data);
}
public function editMessageReplyMarkup($id, $m_id, $kb){
$data=array(
'chat_id' => $id,
'message_id' => $m_id,
'reply_markup' => json_encode(array('inline_keyboard' => $kb))
);
$this->request('editMessageReplyMarkup', $data);
}
public function answerCallbackQuery($cb_id, $message) {
$data = array(
'callback_query_id' => $cb_id,
'text' => $message
);
$this->request('answerCallbackQuery', $data);
}
public function sendChatAction($id,$action='typing') {
$data = array(
'chat_id' => $id,
'action' => $action
);
$this->request('sendChatAction', $data);
}
public function request($method, $data = array()) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.telegram.org/bot' . $this->token . '/' . $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$out = json_decode(curl_exec($curl), true);
curl_close($curl);
return $out;
}
}```
Ответы (1 шт):
Автор решения: ItsOlegDm
→ Ссылка
В итоге сделал так:
public function tgBotMessage($image, $message, $id) {
$bot_url = 'https://api.telegram.org/bot' . $this->token . '/';
$url = $bot_url . 'sendPhoto?chat_id=' . $id ;
$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile($image)
);
$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);
}