Beste leden,
Ik ben bezig met het opzetten van een API voor onze systemen. Hiervoor hanteren we de volgende structuur:
http://api.site.nl/comman...ommand=add¶meters....
Nu wil ik in de command.php de auth uitvoeren en overige dingen controleren of een command en subcommand mogelijk zijn waarna die verder moet gaan in een andere class om het command te verwerken.
Nu ben ik begonnen met het opzetten van de command class (zie onderstaand):
Nu heb ik twee vragen:
- hebben jullie nog opmerkingen mbt bovenstaande opzet;
- hoe kan ik de command functie het beste aanpakken, vanaf hier zal de class opgeroepen moeten worden om bijv. een email adres toe te voegen. Gezien er vele verschillende soorten commando's kunnen zijn zoek ik een zo universele oplossing mogelijk.
Piete
Ik ben bezig met het opzetten van een API voor onze systemen. Hiervoor hanteren we de volgende structuur:
http://api.site.nl/comman...ommand=add¶meters....
Nu wil ik in de command.php de auth uitvoeren en overige dingen controleren of een command en subcommand mogelijk zijn waarna die verder moet gaan in een andere class om het command te verwerken.
Nu ben ik begonnen met het opzetten van de command class (zie onderstaand):
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
| <?php error_reporting(E_ALL); /** * Class command * * */ class Command { /** * User loggedIn * * @acces private * @var integer */ private $loggedIn = NULL; /** * UserId of the user that has loggedin to the API * * @acces private * @var integer */ private $userId; /** * Database connection * * @acces private * @var obj */ private $oDB; /** * Allowed commands * * @acces private * @var array */ private $allowedCommands = array('ADD', 'DEL', 'UPD', 'LIST', 'GET', 'UPGR'); /** * Command * * @acces private * @var array */ private $command; /** * the __construct for alle the commands * * @acces public */ public function __construct() { //connect with the database $oDB = new mysqli('', '', '', ''); $this->oDB = $oDB; //user data check $this->auth($_GET['apiuser'], $_GET['apipass']); //user loggedin correct? if($this->loggedIn == 1) { //check if the command is allowed $this->validateCommand($_GET['command']); } else { throw new Exception('Login data is incorrect!'); } echo $this->command; } /** * auth function to check the user * * @acces private * @var user string * @var pass string */ private function auth($user, $pass) { //check the user and pass $getUser = $this->oDB->query(" SELECT id FROM users WHERE username = '" . mysqli_real_escape_string($user) . "' AND pass = '" . mysqli_real_escape_string(md5($pass)) . "' ") or die (mysqli_error($this->oDB)); //checks if the userdata is correct if($getUser->num_rows == 1) { $listUser = $getUser->fetch_array(); $this->loggedIn = 1; $this->userId = $listUser['id']; } else { $this->loggedIn = NULL; $this->userId = NULL; } } /** * validate the command * * @acces private * @var method string */ private function validateCommand($method) { //check if command is allowed if(in_array(strtoupper($method), $this->allowedCommands)) { $this->command = $method; } else { echo 2; } } /** * setup the sub command * * @acces private */ private function Command() { } } $a = new Command(); |
Nu heb ik twee vragen:
- hebben jullie nog opmerkingen mbt bovenstaande opzet;
- hoe kan ik de command functie het beste aanpakken, vanaf hier zal de class opgeroepen moeten worden om bijv. een email adres toe te voegen. Gezien er vele verschillende soorten commando's kunnen zijn zoek ik een zo universele oplossing mogelijk.
Piete