· 7 years ago · Dec 08, 2018, 08:14 PM
1REST API készÃtése
21. Hozzunk létre egy adatbázist! Egészen pontosan a táblákat. Most egy egyszerű táblát készÃtünk el:
3
4CREATE TABLE IF NOT EXISTS `employee` (
5
6 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
7
8 `employee_name` varchar(255) NOT NULL COMMENT 'employee name',
9
10 `employee_salary` double NOT NULL COMMENT 'employee salary',
11
12 `employee_age` int(11) NOT NULL COMMENT 'employee age',
13
14 PRIMARY KEY (`id`)
15
16)
17
182. Hozzuk létre az erőforrásokat!
19
20
21Route
22
23Method
24
25Type
26
27Posted JSON
28
29Description
30
31/employees
32
33GET
34
35JSON
36
37–
38
39Minden adat
40
41/employees/{id}
42
43GET
44
45JSON
46
47–
48
49Adott id-s
50
51/employees
52
53POST
54
55JSON
56
57{"employee_name": "Malacka", "employee_age": "34", "employee_salary" : "23421"}
58
59Új rekord
60
61/employees/{id}
62
63PUT
64
65JSON
66
67{"employee_name": "Zorro", "employee_age": "100", "employee_salary" : "23421", "id":64}
68
69módosÃtás
70
71/employees/{id}
72
73DELETE
74
75JSON
76
77{"id" : 65}
78
79Törlés
80
81
82
833. Hozzuk létre a REST API-t! Először egy connection.php-t.
84
85
86<?php
87
88
89Class dbObj{
90
91
92/* Database connection */
93
94
95 var $servername = "localhost";
96
97
98var $username = "ksanyi";
99
100
101 var $password = "*****";
102
103
104var $dbname = "user_ksanyi";
105
106
107var $conn;
108
109
110function getConnstring() {
111
112
113 $con = mysqli_connect($this->servername, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error());
114
115
116/* check connection */
117
118
119if (mysqli_connect_errno()) {
120
121
122 printf("Connect failed: %s\n", mysqli_connect_error());
123
124
125 exit();
126
127
128}
129
130
131else { $this->conn = $con; }
132
133
134return $this->conn; } }
135
136?>
137
1384. Most pedig magát az API-t!
139
140
141<?php
142
143
144include("../connection.php");
145
146
147$db = new dbObj(); $connection = $db->getConnstring();
148
149
150$request_method=$_SERVER["REQUEST_METHOD"]; //melyik metódussal hÃvták az API-t?
151
152
153switch($request_method) {
154
155
156 case 'GET':
157
158
159 // GET with id
160
161
162 if(!empty($_GET["id"])) {
163
164
165 $id=intval($_GET["id"]); get_employeesid($id); //eljárás hÃvása, id átadés
166
167
168 }
169
170
171 else {
172
173
174 get_employees(); //all employees, minden adat lekérése
175
176
177 }
178
179
180 break;
181
182
183 case 'POST':
184
185
186 // Insert new employeee with POST
187
188
189 insert_employee(); break;
190
191case 'PUT':
192
193 // Update an employee (with id) and PUT method
194
195 $id=intval($_GET["id"]);
196
197 update_employee($id);
198
199 break;
200
201 case 'DELETE':
202
203 // Delete an employee with ID, DELETE method
204
205 $id=intval($_GET["id"]);
206
207 delete_employee($id);
208
209 break;
210
211 default:
212
213 // Invalid Request Method
214
215 header("HTTP/1.1 405 Method Not Allowed");
216
217 break;
218
219}
220
221//jöhetnek a metódusok...
222
223function get_employees()
224
225{
226
227 global $connection;
228
229 $query="SELECT * FROM employee";
230
231 $response=array();
232
233 $result=mysqli_query($connection, $query);
234
235 while($row=mysqli_fetch_array($result))
236
237 {
238
239 $response[]=$row;
240
241 }
242
243 header('Content-Type: application/json'); //send the header
244
245 echo json_encode($response); //data in JSON format
246
247}
248
249function get_employeesid($id=0) {
250
251 global $connection;
252
253 $query="SELECT * FROM employee";
254
255 if($id != 0) {
256
257 $query.=" WHERE id=".$id." LIMIT 1"; //get employee with a given id
258
259 }
260
261 $response=array();
262
263 $result=mysqli_query($connection, $query);
264
265 while($row=mysqli_fetch_array($result)) {
266
267 $response[]=$row;
268
269 }
270
271 header('Content-Type: application/json'); //header
272
273 echo json_encode($response); //in JSON format }
274
275function insert_employee() {
276
277 global $connection;
278
279 $data = json_decode(file_get_contents('php://input'), true); $employee_name=$data["employee_name"]; //separate them
280
281 $employee_salary=$data["employee_salary"];
282
283 $employee_age=$data["employee_age"];
284
285 echo $query="INSERT INTO employee SET employee_name='".$employee_name."', employee_salary='".$employee_salary."', employee_age='".$employee_age."'";
286
287if(mysqli_query($connection, $query)) {
288
289 $response=array(
290
291 'status' => 1,
292
293 'status_message' =>'Employee Added Successfully.'
294
295 );
296
297 }
298
299 else {
300
301 $response=array(
302
303 'status' => 0,
304
305 'status_message' =>'Employee Addition Failed.'
306
307 );
308
309 }
310
311 header('Content-Type: application/json');
312
313 echo json_encode($response); //response with header
314
315 }
316
317function delete_employee($id) {
318
319 global $connection;
320
321 $query="DELETE FROM employee WHERE id=".$id;
322
323 if(mysqli_query($connection, $query)) {
324
325 $response=array(
326
327 'status' => 1,
328
329 'status_message' =>'Employee Deleted Successfully.'
330
331 );
332
333 }
334
335 else
336
337 {
338
339 $response=array(
340
341 'status' => 0,
342
343 'status_message' =>'Employee Deletion Failed.'
344
345 );
346
347 }
348
349 header('Content-Type: application/json');
350
351 echo json_encode($response);
352
353}
354
355
356
357
358Licensed under the Creative Commons Attribution Share Alike License 4.0