-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
98 lines (77 loc) · 2.35 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
<?php
require_once("Rest.php");
class API extends REST
{
public $data = "";
const DB_SERVER = "127.0.0.1";
const DB_USER = ""; // username here
const DB_PASSWORD = ""; // password here
const DB = ""; // db name here
private $db = NULL;
public function __construct()
{
parent::__construct(); // Init parent contructor
$this->dbConnect(); // Initiate Database connection
}
/*
* Database connection
*/
private function dbConnect()
{
$this->db = mysqli_connect(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD);
if ($this->db)
mysqli_select_db($this->db, self::DB);
}
/*
* Public method for access api.
* This method dynmically call the method based on the query string
*
*/
public function processApi()
{
$func = strtolower(trim(str_replace("/", "", $_REQUEST['rquest'])));
if ((int)method_exists($this, $func) > 0) {
$this->$func();
} else {
// If the method not exist with in this class, response would be "Page not found".
$this->response('Method not found', 404);
}
}
/*
* Simple login API
* Login must be POST method
*
*/
private function login()
{
// Cross validation if the request method is POST else it will return "Not Acceptable" status
if ($this->get_request_method() != "POST") {
$this->response('', 406);
}
$username = $this->_request['username'];
$password = $this->_request['password'];
// Input validations
$sql = $this->db->query("SELECT * FROM Users WHERE username = '$username' AND password = '$password' LIMIT 1");
if (mysqli_num_rows($sql) > 0) {
$emparray = array();
while ($row = mysqli_fetch_assoc($sql)) {
$emparray[] = $row;
}
$this->response($this->json($emparray), 200);
} else {
$this->response('No Data found', 204);
}
}
/*
* Encode array into JSON
*/
private function json($data)
{
if (is_array($data)) {
return json_encode($data);
}
}
}
// Initiiate Library
$api = new API;
$api->processApi();