← Вернуться к списку тем

Отправить несколько файлов

Подскажите, в чём у меня косяк?
Вместо картинок хочу отправлять файлы.


$token = "xxxxxxxxxxxxxxxxxxxxxx";
$chat_id = xxxxxxxxx';
$dir = "./files";
$path = "/Folder/files/";

// сканируем директорию - удаляем служебные пути ./ и ../
$files = array_slice(scandir($dir), 2);
$url = "https://api.telegram.org/bot" . $token . "/sendMediaGroup";

$type = "document";
// $postContent = array();

foreach ($files as $file) {
$media[] = [
'type' => $type,
'media' => "attach://" . $file,
];

$postContent[] = [
$file => new CURLFile("https://" . $_SERVER['SERVER_NAME'] . $path . $file)
];
}

$postContent[] = [
'chat_id' => $chat_id,
'media' => json_encode($media),
];

echo (json_encode($postContent));

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;
// Echo(json_encode($result));
}

Ответы