-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.php
executable file
·189 lines (165 loc) · 5.8 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
require_once("Rest.inc.php");
class API extends REST {
public $data = "";
const DB_SERVER = "";
const DB_USER = "";
const DB_PASSWORD = "";
const DB = "";
private $db = NULL;
private $mysqli = NULL;
public function __construct(){
parent::__construct(); // Init parent contructor
$this->dbConnect(); // Initiate Database connection
}
/*
* Connect to Database
*/
private function dbConnect(){
$this->mysqli = new mysqli(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD, self::DB);
}
/*
* Dynmically call the method based on the query string
*/
public function processApi(){
$func = strtolower(trim(str_replace("/","",$_REQUEST['x'])));
if((int)method_exists($this,$func) > 0)
$this->$func();
else
$this->response('',404); // If the method not exist with in this class "Page not found".
}
private function login(){
if($this->get_request_method() != "POST"){
$this->response('',406);
}
$email = $this->_request['email'];
$password = $this->_request['pwd'];
if(!empty($email) and !empty($password)){
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
/** select your query here **/
$query="";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0) {
$result = $r->fetch_assoc();
// If success everythig is good send header as "OK" and user details
$this->response($this->json($result), 200);
}
$this->response('', 204); // If no records "No Content" status
}
}
$error = array('status' => "Failed", "msg" => "Invalid Email address or Password");
$this->response($this->json($error), 400);
}
private function customers(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
/** select your query here **/
$query="";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0){
$result = array();
while($row = $r->fetch_assoc()){
$result[] = $row;
}
$this->response($this->json($result), 200); // send user details
}
$this->response('',204); // If no records "No Content" status
}
private function customer(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$id = (int)$this->_request['id'];
if($id > 0){
/** select your query here **/
$query="";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0) {
$result = $r->fetch_assoc();
$this->response($this->json($result), 200); // send user details
}
}
$this->response('',204); // If no records "No Content" status
}
private function insertCustomer(){
if($this->get_request_method() != "POST"){
$this->response('',406);
}
// $customer = json_decode(file_get_contents("php://input"),true);
$customer = $this->_request;
$column_names = array('customerName', 'email', 'city', 'address', 'country');
$keys = array_keys($customer);
$columns = '';
$values = '';
foreach($column_names as $desired_key){ // Check the customer received. If blank insert blank into the array.
if(!in_array($desired_key, $keys)) {
$$desired_key = '';
}else{
$$desired_key = $customer[$desired_key];
}
$columns = $columns.$desired_key.',';
$values = $values."'".$$desired_key."',";
}
/** insert your query here **/
$query = "INSERT INTO customers(".trim($columns,',').") VALUES(".trim($values,',').")";
if(!empty($customer)){
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
$success = array('status' => "Success", "msg" => "Customer Created Successfully.", "data" => $customer);
$this->response($this->json($success),200);
}else
$this->response('',204); //"No Content" status
}
private function updateCustomer(){
if($this->get_request_method() != "POST"){
$this->response('',406);
}
$customer = json_decode(file_get_contents("php://input"),true);
$id = (int)$customer['id'];
$column_names = array('customerName', 'email', 'city', 'address', 'country');
$keys = array_keys($customer['customer']);
$columns = '';
$values = '';
foreach($column_names as $desired_key){ // Check the customer received. If key does not exist, insert blank into the array.
if(!in_array($desired_key, $keys)) {
$$desired_key = '';
}else{
$$desired_key = $customer['customer'][$desired_key];
}
$columns = $columns.$desired_key."='".$$desired_key."',";
}
$query = "UPDATE customers SET ".trim($columns,',')." WHERE customerNumber=$id";
if(!empty($customer)){
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
$success = array('status' => "Success", "msg" => "Customer ".$id." Updated Successfully.", "data" => $customer);
$this->response($this->json($success),200);
}else
$this->response('',204); // "No Content" status
}
private function deleteCustomer(){
if($this->get_request_method() != "DELETE"){
$this->response('',406);
}
$id = (int)$this->_request['id'];
if($id > 0){
/** select your query here **/
$query="";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
$success = array('status' => "Success", "msg" => "Successfully deleted one record.");
$this->response($this->json($success),200);
}else
$this->response('',204); // If no records "No Content" status
}
/*
* Encode array into JSON
*/
private function json($data){
if(is_array($data)){
return json_encode($data);
}
}
}
// Initiiate Library
$api = new API;
$api->processApi();
?>