<?php
include('db.php');

$json = file_get_contents('php://input');
file_put_contents('New_SMS.json', $json);

$input = file_get_contents("New_SMS.json");
parse_str($input, $output); // works because data is query string, not JSON

$Sender   = mysqli_real_escape_string($conn, $output['From']);
$Receiver = mysqli_real_escape_string($conn, $output['To']);
$Message  = mysqli_real_escape_string($conn, preg_replace('/[^A-Za-z0-9 ]/', '', $output['Body']));
$Message_ID = mysqli_real_escape_string($conn, $output['MessageSid']);
$current_date_time = date("Y-m-d H:i:s");

$Query = "INSERT INTO SMS 
    (Sender, Receiver, Body, Status, Type, Message_Id, Seen, created_at, updated_at)
    VALUES ('$Sender', '$Receiver', '$Message', 'Success', 'Received', '$Message_ID', '0', '$current_date_time', '')";

$Insert = mysqli_query($conn, $Query);

if ($Insert) {
    echo "Inserted successfully.";
} else {
    echo "Error: " . mysqli_error($conn);
}
?>
