网站上已设置了一个表格,其中的数据将发送到电报机器人。 PHP脚本是这样的
<?php
echo $TOKEN;
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
use Dotenv\Dotenv;
// Specify the path to the directory where the .env file is located
$dotenvPath = __DIR__;
// Create an instance of Dotenv and load environment variables
$dotenv = Dotenv::createImmutable(dirname(__DIR__));
$dotenv->load();
// Get the token value
$TOKEN = $_ENV['TOKEN'];
// Telegram bot token and channel ID
$TELEGRAM_CHAT_ID = "-1002017740876";
// Check for the presence of a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect data from the form
$product = $_POST['product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = $_FILES['attachments'];
$invoice = $_FILES['invoice'];
// Save data to the database or file (depends on your requirements)
// Send notification to Telegram
$message = "<b>Promindustry</b>\n";
$message .= "<b>Product Name: </b>".$product." \n";
$message .= "<b>Sender: </b>".$name." \n";
$message .= "<b>Phone: </b>".$phone." \n";
$message .= "<b>Email: </b>".$email;
file_get_contents("https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$TELEGRAM_CHAT_ID&parse_mode=html&text=".urlencode($message));
// Function to send a file to Telegram
function sendFileToTelegram($document, $caption = '') {
global $TOKEN, $TELEGRAM_CHAT_ID;
$url = "https://api.telegram.org/bot$TOKEN/sendDocument";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'chat_id' => $TELEGRAM_CHAT_ID,
'document' => new CURLFile($document['tmp_name'], $document['type'], $document['name']),
'caption' => $caption
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
}
// Send files to Telegram
if ($attachments['error'] == UPLOAD_ERR_OK) {
sendFileToTelegram($attachments, 'Attachment: Signboard, part photo, other information');
}
if ($invoice['error'] == UPLOAD_ERR_OK) {
sendFileToTelegram($invoice, 'Attachment: Details for invoicing');
}
// Send a response to the client (e.g., JSON response)
echo json_encode([ "status" => "success", "message" => "Form submitted successfully!", ]);
} else {
// Send a response to the client about the invalid request method
echo json_encode(["status" => "error", "message" => "Invalid request method!"]);
}
?>
问题是只要文件大小小于 50 KB,一切都可以正常工作。一旦文件较大,就不会发送表格。 我将非常感谢您帮助解决这个问题。
我尝试重做脚本
此类行为最可能的原因可能是您的 php.ini 配置。
请找到您的 php.ini 文件并根据您的要求编辑这两行。
upload_max_filesize = 64M (change size as per your project requirement)
post_max_size = 64M
还验证 telegram api 的文件限制(如果有)