此代码将数据写入文件
$data = file_get_contents('php://input');
$data = json_decode($data, true);
function writeLogFile($string, $clear = false){
$log_file_name = __DIR__."/message.txt";
$now = date("Y-m-d H:i:s");
if($clear == false) {
file_put_contents($log_file_name, $now." ".print_r($string, true)."\r\n", FILE_APPEND);
} else {
file_put_contents($log_file_name, $now." ".print_r($string, true)."\r\n");
}
}
writeLogFile($data, false);
这种格式
...
2025-03-11 01:10:28 Array
(
[update_id] => 82576514
[message] => Array
(
[message_id] => 6618
[from] => Array
(
[id] => 390344561
[is_bot] =>
[first_name] => 𝕊𝔹𝔹
[last_name] => 𝕚𝕟𝕘
[username] => ETR
[language_code] => ru
)
[chat] => Array
(
[id] => 390344561
[first_name] => 𝕊𝔹𝔹
[last_name] => 𝕚𝕟𝕘
[username] => ETR
[type] => private
)
[date] => 1741644628
[text] => Главная
)
)
...
如何使文件以这种格式写入
2025-03-11 01:10:28 Имя => 𝕊𝔹𝔹, Фамилия => 𝕚𝕟𝕘, Ник => ETR ...
以及在.csv文件中(如何设置此代码?)
$buffer = fopen(__DIR__ . '/file.csv', 'w');
fputs($buffer, chr(0xEF) . chr(0xBB) . chr(0xBF));
foreach($string as $val) {
fputcsv($buffer, $val, ';');
}
fclose($buffer);
exit();
1 个回答