-
Notifications
You must be signed in to change notification settings - Fork 14
/
php-send-message.php
40 lines (31 loc) · 1.03 KB
/
php-send-message.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
<?php
include_once 'php-db.php';
// get the input
$name = trim(htmlspecialchars($_POST['name'] ?? ''));
$message = trim(htmlspecialchars($_POST['message'] ?? ''));
if (!$name || !$message)
die;
// insert into the database
$stmt = $mysqli -> prepare('INSERT INTO chat_messages (name, time, message) VALUES (?,?,?)');
$time = time();
$stmt -> bind_param('sis', $name, $time, $message);
$stmt -> execute();
// Send the HTTP request to the websockets server (it runs both HTTP and Websockets)
// (change the URL accordingly)
$ch = curl_init('http://localhost:8080');
// It's POST
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// we send JSON encoded data to the client
$jsonData = json_encode([
'name' => $name,
'message' => $message
]);
$query = http_build_query(['data' => $jsonData]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
// Just return the transfer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute
$response = curl_exec($ch);
// close
curl_close($ch);
// Now real time notification should be sent to all the users