This PHP script allow you to create a simple and universal REST API. Based on the original idea of Maurits van der Schee, Creating a simple REST API in PHP.
It use a MySQL Database.
uni-api support HTTP verbs GET, POST, PATCH, PUT and DELETE.
It have an authentication module.
-
Save the file api.php and /core folder in your server document root
-
configure the
config.php
file with the requested parameters -
Configure the database's models in
models.json
- the main use it will be automatically created with admin as username and password
-
launch
install.php
from the browser -
Check if there is any errors while the installation
There is a models already written by me in the project folder.
The usage is realy simple:
POST api.php
/user
Logs user into the system
request:
{
"username": "foo",
"password": "foopass"
}
response:
{
"username": "foo",
"id": "foo_id",
"token": "access_token"
}
PATCH api.php
/user/:id
Update user
request: (you can send just username, password or both)
{
"username": "new_foo_username",
"password": "new_foo_pass"
}
response:
{
"count": "# of the rows affected"
}
GET api.php
/table
Get all the rows
response:
[
{
//row_one
},
{
//row_two
}
]
GET api.php
/table/:id
Find row by ID
response:
{
//all table's fields
}
PUT api.php
/table/:id
Update a row in the database
Need to send the access_token in the Authorization header
request:
{
//row's fields i want to update
}
response:
{
"count": "# of the rows affected"
}
POST api.php
/table
Create a new row
Need to send the access_token in the Authorization header
request:
{
//table's field without id and update_date
}
response:
{
"count": "# of the rows affected"
}
DELETE api.php
/table/:id
Create a new row
Need to send the access_token in the Authorization header
response:
{
"count": "# of the rows affected"
}
GET api.php
/table/:id/relatedTable
Delete
- Make a GET request to the table;
- Get the relatedTable_id from the body;
- Make a GET request to the relatedTeble with the obtained id.
PUT api.php
/table/:id/relatedTable
Create a new relation
Need to send the access_token in the Authorization header
- Just update the relatedTable_id field with the related element's id.
DELETE api.php
/table/:id/relatedTable
Create a new row
Need to send the access_token in the Authorization header
- Just set to NULL the relatedTable_id field
GET api.php
/table/:id/relatedTable
Get the realted elements of a determinate table
Need to send the access_token in the Authorization header
response:
{
[
{
//row_one
},
{
//row_two
}
]
}
POST api.php
/table/:id/relatedTable
Relate an existent element to the table
Need to send the access_token in the Authorization header
request:
{
// an existent element to relate
}
response:
{
"count": "# of the rows affected"
}
DELETE api.php
/table/:id/relatedTable/:relatedId
Delete a relation between two elements
Need to send the access_token in the Authorization header
response:
{
"count": "# of the rows affected"
}
- User
- User permission
- Custom user
- Better Multi User support
- Files
- Files support
- Models
- At State of Art you can't delete models or update it after. I want to make that possible.
- Better configuration of the models. (I.E.: Required/Not Required fields)
- Example
- Write a complete example.