-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.php
More file actions
71 lines (58 loc) · 2.24 KB
/
Copy pathhandler.php
File metadata and controls
71 lines (58 loc) · 2.24 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the raw POST data as JSON
$rawPostData = file_get_contents('php://input');
// Decode the JSON data
$postData = json_decode($rawPostData, true);
// Check if the JSON decoding was successful
if ($postData === null) {
http_response_code(400); // Bad Request
echo "Invalid JSON data.";
} else {
// Extract the "message" and "date" fields
$message = $postData['message'];
$date = $postData['data']['date'];
// Format the date
$formattedDate = date('d/m/Y, H:i:s', strtotime($date));
// Echo the message and formatted date
//echo "Message: $message<br>";
//echo "Formatted Date: $formattedDate";
$url = "https://rocket.chat.com/api/v1/chat.postMessage"; // Your RocketChat Server URL
// Create POST data as an associative array
$data = [
'roomId' => 'RoomName',
'text' => $message . ' - ' . $formattedDate,
];
// Create headers as an associative array with Bot Token and ID
$headers = [
'X-Auth-Token: XXXXX',
'X-User-Id: XXXXX',
'Content-Type: application/json',
];
// Convert the data to JSON
$jsonData = json_encode($data);
// Create a context for the stream with headers
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => $jsonData,
],
]);
// Perform the POST request and capture the response
$response = file_get_contents($url, false, $context);
// Check for errors
if ($response === false) {
echo 'Error making POST request.';
} else {
// Handle the response (you can print or process it as needed)
echo $response;
}
}
} else {
// If the request method is not POST, return an error message
http_response_code(405); // Method Not Allowed
echo "This endpoint only accepts POST requests.";
}
?>