我正在服务器上创建一个回调URL。我需要将file_get_contents
保存在日志文件和MySQL数据库中。
该日志文件还可以,因为我能够记录和查看解码后的数据,但是我的数据库中也需要相同的文件。
try {
header("Content-Type:application/json");
$postData = file_get_contents('php://input');
$filePath = "\messages.log";
//error log
$errorLog = "\errors.log";
$decodedData = json_decode($postData, true);
$service_id = $decodedData['service_id'];
$trans_id = $decodedData['trans_id'];
$status = $decodedData['status'];
$partner = $decodedData['partner'];
$con = mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO test_table (service_id, trans_id, status,
partner)
VALUES
('$service_id', '$trans_id', '$status', '$partner' )";
if (!mysqli_query($con, $sql)) {
echo mysqli_error($con);
}
$file = fopen($filePath, "a");
fwrite($file, $postData);
fwrite($file, "\r\n");
} catch (Exception $ex) {
//appending exception to file
$logErr = fopen($errorLog, "a");
fwrite($logErr, $ex->getMessage());
fwrite($logErr, "\r\n");
fclose($logErr);
}
fwrite($file, "\r\n");
fclose($file);
日志文件中没有错误,并且数据库中没有任何内容