-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·125 lines (103 loc) · 3.04 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @author Nicholas Sardo
*
* quick and dirty reference sketch on using the Flight Micro-Framework for developing
* an HTTP Rest API in PHP
*
*/
require 'flight/Flight.php';
require 'db.php';
/* set an instance of database w/in Flight Framework */
Flight::set( 'd', DB::get_Instance() );
/**
* check credentials before allowing access to API
*/
Flight::before( 'validate', function( &$params, &$output ){
#rough sketch for validation
#$hash = hmac( 'sha1', $_POST['json'].$_POST['time'].$_POST['public-key'], $secret );
#if ( $hash === $_POST['hash']){
#proceed
#} else {
# sendResponse( 401, 'Unauthorized' );
#}
});
/**
* retrieve data about a student
* Example URL: localhost/student/1
*/
Flight::route('GET /student/@id:[0-9]+', function( $id ){
$d = Flight::get('d');
Flight::json( array( $d->quer( $id ) ) );
});
/**
* create new students contact info
* Example URL: localhost/student/new/contact
* send record in body as json
*/
Flight::route('POST /student/new/contact', function(){
$d = Flight::get('d');
#send json coming in body to the db
$d->createContactInfo( Flight::request()->body );
});
/**
* retrieve data about students courses
* Example URL: localhost/student/1/courses
*/
Flight::route('GET /student/@id:[0-9]+/courses', function( $id ){
$d = Flight::get( 'd' );
$c = $d->getStudentCourses( $id );
#return json
Flight::json( array( $c ) );
});
/**
* retrieve data about students grade for specific course
* Example URL: localhost/student/1/grade/course_name
*/
Flight::route('GET /student/@id:[0-9]+/grade/@course/', function( $id, $course ){
$d = Flight::get( 'd' );
$g = $d->getGrade( $id, $course );
#return json
Flight::json( array( $g ) );
});
/**
* insert data student class-info and grade
* Example URL: localhost/student/1/course_name/letter_grade/start_date/end_date
* prob. should be json record sent via POST request
*/
Flight::route('PUT /student/@id:[0-9]+/create/@course/@grade/@start/@end', function( $id, $course, $grade, $start, $end ){
$d = Flight::get( 'd' );
$d->createStudentCourse( $id, $course, $grade, $start, $end );
});
/**
* update data student grade
* Example URL: localhost/student/1/course_name/new_grade
*/
Flight::route('POST /student/@id:[0-9]+/@course/@grade', function( $id, $course, $grade ){
$d = Flight::get( 'd' );
$d->updateGrade( $id, $course, $grade );
});
/**
* EXAMPLE CODE FOR PUT REQUEST
* Obtain variables w/in body of PUT Request
*/
Flight::route('PUT /', function(){
if ( $_SERVER['REQUEST_METHOD'] == "PUT" ){
#separate key=value pairs in request body
parse_str(Flight::request()->body, $put_vars );
/* doesn't work because this is already done in Flight */
#parse_str( file_get_contents('php://input'), $post_vars );
#show that keys/values are now separated
print_r( $put_vars );
}
});
#EXAMPLE ROUTE
Flight::route('GET /', function(){
echo 'not implemented.' . "\n";
});
#EXAMPLE: add foo=bar to request body, and send POST request
Flight::route('POST /', function(){
echo Flight::request()->data['foo'];
});
Flight::start();
?>