-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
62 lines (51 loc) · 1.37 KB
/
api.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
header("Content-Type:application/json");
require "data.php";
function index(){
//Only Allow user access on /api and not /api.php
if( $_SERVER['REQUEST_URI'] == '/api.php'){
response(403,"Forbidden",NULL);
}
}
function apicall(){
//Retrieve all json values
$data = json_decode(file_get_contents('php://input'), true);
//Check if all values are set, if yes create new score
if(isset($data['id']) && isset($data['title']) && isset($data['score']))
{
//Validate all values
if(is_numeric ($id=$data['id']) && is_string($data['title']) && is_float ($data['score'])){
$id=$data['id'];
$title = $data['title'];
$score = $data['score'];
create_score($id,$title, $score);
}else{
//Invalid values send teapot response code :)
response(418,"Sorry you have not sent correct data",NULL);
}
}
else
{
//Search for score by id
if(isset($data['id']))
{
$id=$data['id'];
get_score($id);
}
//Search for score without id
else
{
get_all_scores();
}
}
}
function response($status,$status_message,$data)
{
header("HTTP/1.1 ".$status_message);
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
index();