-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.php
161 lines (115 loc) · 4.77 KB
/
tester.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
API Demo
This script provides a RESTful API interface for a web application
Input:
$_GET['format'] = [ json | html | xml ]
$_GET['method'] = []
Output: A formatted HTTP response
Author: Mark Roland
History:
11/13/2012 - Created
*/
// --- Step 1: Initialize variables and functions
/**
* Deliver HTTP Response
* @param string $format The desired HTTP response content type: [json, html, xml]
* @param string $api_response The desired HTTP response data
* @return void
**/
function deliver_response($format, $api_response){
// Define HTTP responses
$http_response_code = array(
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found'
);
// Set HTTP Response
header('HTTP/1.1 '.$api_response['status'].' '.$http_response_code[ $api_response['status'] ]);
// Process different content types
if( strcasecmp($format,'json') == 0 ){
// Set HTTP Response Content Type
header('Content-Type: application/json; charset=utf-8');
// Format data into a JSON response
$json_response = json_encode($api_response);
// Deliver formatted data
echo $json_response;
}elseif( strcasecmp($format,'xml') == 0 ){
// Set HTTP Response Content Type
header('Content-Type: application/xml; charset=utf-8');
// Format data into an XML response (This is only good at handling string data, not arrays)
$xml_response = '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<response>'."\n".
"\t".'<code>'.$api_response['code'].'</code>'."\n".
"\t".'<data>'.$api_response['data'].'</data>'."\n".
'</response>';
// Deliver formatted data
echo $xml_response;
}else{
// Set HTTP Response Content Type (This is only good at handling string data, not arrays)
header('Content-Type: text/html; charset=utf-8');
// Deliver formatted data
echo $api_response['data'];
}
// End script process
exit;
}
// Define whether an HTTPS connection is required
$HTTPS_required = FALSE;
// Define whether user authentication is required
$authentication_required = FALSE;
// Define API response codes and their related HTTP response
$api_response_code = array(
0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),
1 => array('HTTP Response' => 200, 'Message' => 'Success'),
2 => array('HTTP Response' => 403, 'Message' => 'HTTPS Required'),
3 => array('HTTP Response' => 401, 'Message' => 'Authentication Required'),
4 => array('HTTP Response' => 401, 'Message' => 'Authentication Failed'),
5 => array('HTTP Response' => 404, 'Message' => 'Invalid Request'),
6 => array('HTTP Response' => 400, 'Message' => 'Invalid Response Format')
);
// Set default HTTP response of 'ok'
$response['code'] = 0;
$response['status'] = 404;
$response['data'] = NULL;
// --- Step 2: Authorization
// Optionally require connections to be made via HTTPS
if( $HTTPS_required && $_SERVER['HTTPS'] != 'on' ){
$response['code'] = 2;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = $api_response_code[ $response['code'] ]['Message'];
// Return Response to browser. This will exit the script.
deliver_response($_GET['format'], $response);
}
// Optionally require user authentication
if( $authentication_required ){
if( empty($_POST['username']) || empty($_POST['password']) ){
$response['code'] = 3;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = $api_response_code[ $response['code'] ]['Message'];
// Return Response to browser
deliver_response($_GET['format'], $response);
}
// Return an error response if user fails authentication. This is a very simplistic example
// that should be modified for security in a production environment
elseif( $_POST['username'] != 'foo' && $_POST['password'] != 'bar' ){
$response['code'] = 4;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = $api_response_code[ $response['code'] ]['Message'];
// Return Response to browser
deliver_response($_GET['format'], $response);
}
}
// --- Step 3: Process Request
// Method A: Say Hello to the API
if( strcasecmp($_GET['method'],'hello') == 0){
$response['code'] = 1;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = 'Hello World';
}
// --- Step 4: Deliver Response
// Return Response to browser
deliver_response($_GET['format'], $response);
?>