使用电报Bot PHP sendMediaGroup方法
我试图用我的电报机器人将一些本地图像发送到一条消息中。我使用了sendMediaGroup方法进行发送,但不幸的是不起作用。这是我的密码:使用电报Bot PHP sendMediaGroup方法,php,bots,telegram,Php,Bots,Telegram,我试图用我的电报机器人将一些本地图像发送到一条消息中。我使用了sendMediaGroup方法进行发送,但不幸的是不起作用。这是我的密码: <?php $url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup"; $postContent = [ 'chat_id' => $GLOBALS['chatId'], 'media' =&
<?php
$url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup";
$postContent = [
'chat_id' => $GLOBALS['chatId'],
'media' => json_encode([
['type' => 'photo', 'media' => new \CURLFile(__DIR__ . '/img1.png')], // Not working
['type' => 'photo', 'media' => new \CURLFile('attach://' . __DIR__ . 'jpg1.png')], // Not working
['type' => 'video', 'media' => new CURLFile(realpath('mp4.mp4'))], // Not working
])
];
post($url, $postContent);
function post($url, $postContent)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
要使用电报机器人API的
sendMediaGroup
方法上载媒体,您需要使用attach://fileName
way,并将文件内容附加到您的请求中。大概是这样的:
$postContent=[
'chat_id'=>$GLOBALS['chatId'],
“媒体”=>json_编码([
['type'=>'照片','media'=>'attach://file1.png' ],
['type'=>'照片','media'=>'attach://file2.png' ],
]),
'file1.png'=>新文件(realpath($filePath1)),
'file2.png'=>新文件(realpath($filePath2)),
];
post($url,$postContent);
成功了!非常感谢。但是如何使用while循环遍历它们对于每个文件,将文件添加到$postContent
和一个临时变量$mediaArray
。然后,设置$postContent['media']=json_encode($mediarray)代码>