Handling Incoming Webhooks and Inserting Into DB

1.1k Views Asked by At

I am currently working on a script within PHP that monitors the outage of a service. This is the first time I am using PhP and am not 100% familiar with it. So I'm sorry if the answer is obvious.

I am using Uptime Kuma to monitor, when a service goes offline, it sends a web hook to my server address http://192.168.1.100/controller/botmonitor.php.

I have been looking around and unable to find out how to get it to insert the data into a MySQL database. So fair, I have got the following:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $data = file_get_contents('php://input');
    $event = json_decode($data, true);
    file_put_contents("log.txt", $event["monitor"]["name"], true);
    $global $conn;
    $error = "error";
    $errTitle = $event["monitor"]["name"];
    $errMsg = "test";
    $stmt = $conn->prepare("INSERT INTO serviceerror (errorType, errorTitle, errorMsg) VALUES (?, ?, ?)");
    /* Bind Parameters */        
    $stmt->bind_param("sss", $errTitle, $error, $errMsg);
    /* Exceute SQL Query */
    $stmt->execute();
    /* Close Statement */
    $stmt->close();

}

The file_put_contents("log.txt", $event["monitor"]["name"], true); section works perfectly. Inserts the name of the serive without any issue.

The Json Array from the web hook is as follows:

Array
(
    [heartbeat] => Array
        (
            [monitorID] => 10
            [status] => 1
            [time] => 2022-01-25 23:40:17
            [msg] => 200 - OK
            [ping] => 23
            [important] => 1
            [duration] => 20
        )

    [monitor] => Array
        (
            [id] => 10
            [name] => Service
            [url] => https://website.com
            [method] => GET
            [body] => 
            [headers] => 
            [basic_auth_user] => 
            [basic_auth_pass] => 
            [hostname] => 
            [port] => 
            [maxretries] => 0
            [weight] => 2000
            [active] => 1
            [type] => http
            [interval] => 20
            [retryInterval] => 20
            [keyword] => 
            [ignoreTls] => 
            [upsideDown] => 
            [maxredirects] => 10
            [accepted_statuscodes] => Array
                (
                    [0] => 200-299
                )

            [dns_resolve_type] => A
            [dns_resolve_server] => 1.1.1.1
            [dns_last_result] => 
            [pushToken] => 
            [notificationIDList] => Array
                (
                    [1] => 1
                    [3] => 1
                    [4] => 1
                )

            [tags] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [monitor_id] => 10
                            [tag_id] => 1
                            [value] => 
                            [name] => Local Server
                            [color] => #D97706
                        )

                )

        )

    [msg] => [Service] [✅ Up] 200 - OK
)

My database connection file that is located localhost/includes/db_connection.php contains:


$config = include("config.php");
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
/* Sets DB veriables based on the config.php file */
$host = $config->DBHost;
$user = $config->DBUser;
$pass = $config->DBPass;
$db_name = $config->DBName;

/* Tries to make conenction to the DB */

$conn = @new MySQLi($host, $user, $pass, $db_name);

/* Checks for errors during the connecting of the DB and sets a session variable */
if ($conn->connect_error) {
    $error = $conn->connect_error;
    $_SESSION['dbError'];
} else{
/* unsets the session variable if connection is made to stop any possible problems */
    unset($_SESSION['dbError']);
}

Database is working fine for other queries, I am just unable to get it to work when this web socket is sent. Database is also being called because if I put a insert outside of the if statement, it inserts it no problems at all.

Any help would be really appreciated.

1

There are 1 best solutions below

0
On

After spending hours upon hours, I thought I'd ask on here... Annoyingly, I figured it out not long after..

Updated "botmonitor.php" script is as follows:

$data = file_get_contents('php://input');
$event = json_decode($data, true);
file_put_contents("log.txt", $event["monitor"]["name"], true);
$errorType = "error";
$errorTitle = $event["monitor"]["name"];
$errorMsg = "Test";

$stmt = $conn->prepare("INSERT INTO serviceerror (errorType, errorTitle, errorMsg) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $errorType, $errorTitle, $errorMsg);
$stmt->execute();
$stmt->close();