-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdht.php
46 lines (34 loc) · 1.28 KB
/
dht.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "test";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Takes raw data from the request
$json = file_get_contents('php://input');
// echo $json;
// // Converts it into a PHP object
$data = json_decode($json);
if (json_last_error() == JSON_ERROR_NONE) {
$device_id = $data->device_id;
$type = $data->type;
$value = $data->value;
$created = date('Y-m-d H:i:s', strtotime(' + 4 hours'));
$query = "INSERT INTO events (device_id, type, value, created) VALUES ('$device_id','$type','$value','$created');";
if (mysqli_query($conn, $query)) {
$response = ['response' => 'New data created successfully :)'];
header("HTTP/1.1 201 OK");
} else {
$response = ['response' => "Error: " . $query . "<br>" . mysqli_error($conn)];
header("HTTP/1.1 500 NOT OK");
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
} else
echo "JSON Invalid";
}
?>