-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
54 lines (44 loc) · 1.54 KB
/
chat.php
File metadata and controls
54 lines (44 loc) · 1.54 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
<?php
require('db.php');
$db = db_connect();
header("Content-Type: application/json; charset=UTF-8");
/*
TODO:
Implement endpoints described in the lab writeup
Hints:
- Use $_SERVER["REQUEST_METHOD"] to detect if a request is a GET or POST request
- If it's a GET request, you can use empty($_GET) to check if there are no GET parameters
- Look at database.php to see what's implemented for you...
*/
if ($_SERVER["REQUEST_METHOD"] == 'GET') {
// GET Endpoints
if (!isset($_GET['last_id'])) {
// Endpoint for getting the complete chat history
// GET /chat.php
$result = get_chats(-1, $_GET['leagueid']);
$myJSON = json_encode($result);
echo $myJSON;
} else {
// Endpoint for getting new chats since given last chat id
// GET /chat.php?last_id=#
$result = get_chats($_GET['last_id'], $_GET['leagueid']);
$myJSON = json_encode($result);
echo $myJSON;
}
}
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
// Endpoint for user sending new chat
// POST /chat.php
$_POST = json_decode(file_get_contents('php://input'), true);
//$request = json_decode($_POST, false);
$result = insert_chat($_POST['user'], $_POST['message'], $_POST['leagueid']);
$myJSON = json_encode($result);
echo $myJSON;
// Hint: If using JSON data for POST,
// need to populate $_POST superglobal yourself with:
// $_POST = json_decode(file_get_contents('php://input'), true);
// Hint: Use JSON data, form data is restrictive
}
// Set content type to json, and output json in page to be sent
db_disconnect();
?>