Simple PHP API v.1.0
This API aims to present a brief to consume a API resources, mainly for students in the early years of Computer Science courses and the like. For this reason, it has only three EndPoints (resources) to use, and can be expanded according to the need.
As it is an instructional project, it is not recommended that it be applied in a production environment, as safety routines and tests have not been implemented. These resources must be researched and implemented, following the current rules, in addition to good practices. Built in PHP 7 (see below), it allows the beginner to understand the mechanisms of access to the resources of an API.
PHP 7.3.26 (cli) (built: Jan 5 2021 15:10:35) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.26, Copyright (c) 1998-2018 Zend Technologies
How to use this content?
This content has free license for use (CC BY-SA 4.0). If you want, collaborate in this repository any improvements that have been made.
Composer
Changes should be updated via composer dump-autoload -o
on your local machine.
Documentation
Below are shown the resources available for using the API. After installation on the server, you can use the examples on how to use API data with: Node.js, PHP and Python, or try online.
Structure
+---api
| \---new
| \---search
| \---update
+---src
| \---Database
| \---Helpers
| \---User
\---vendor
\---composer
Database
The development uses the MySQL 5 database, which can be changed at any time according to the need for use. The database should be configured in Database\Database.php
Scripts SQL
CREATE DATABASE <name>;
CREATE TABLE users (
id INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL
)
Token
To use the API, include the access Token in src\User\UserModel.php
method auth()
. A suggestion for improvement this API is a Token distribution system.
Resources
api/new/
POST: url = 'http://URI/api/new/';
payload = {
"name": "name",
"email": "email",
"username": "username",
"password": "password"
}
header = {
"content-type": "application/json",
"Authorization": "YOUR_TOKEN"
}
Success
[
{
"status": "101",
"info": "User Successfully Added"
}
]
Warnings
[
{
"status": "102",
"info": "Invalid Arguments Number (Expected Four)"
}
]
[
{
"status": "103",
"info": "Could Not Add User"
}
]
[
{
"status": "104",
"info": "User Already Exists"
}
]
api/search/
POST: url = 'http://URI/api/search/';
payload = {
"username": "username",
"password": "password"
}
header = {
"content-type": "application/json",
"Authorization": "YOUR_TOKEN"
}
Success
[
{
"status": "201",
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
]
Warnings
[
{
"status": "202",
"info": "Invalid Arguments Number (Expected Two)"
}
]
[
{
"status": "203",
"info": "User Not Found or Incorrect username and/or password"
}
]
api/update/
PUT: url = 'http://URI/api/update/';
payload = {
"name": "name",
"email": "email",
"username": "username",
"password": "password"
}
header = {
"content-type": "application/json",
"Authorization": "YOUR_TOKEN"
}
Success
[
{
"status": "301",
"info": "User Successfully Updated'
}
]
Warnings
[
{
"status": "302",
"info": "Invalid Arguments Number (Expected Four)"
}
]
[
{
"status":"303",
"info": "User Not Found or Incorrect username and/or password"
}
]
[
{
"status": "304",
"info": "Could Not Update User"
}
]
Other Warnings
[
{
"status": "400",
"info": "Bad Request (Invalid Syntax)"
}
]
[
{
"status": "401",
"info": "Token Refused"
}
]
[
{
"status": "402",
"info": "Invalid or Missing Token"
}
]
[
{
"status": "403",
"info": "Payload Precondition Failed"
}
]
[
{"status": "404",
"info": "Method Not Allowed"
}
]
[
{"status": "405",
"info": "<SQL Code>"
}
]
[
{"status": "406",
"info": "<Unknow>"
}
]
Try Online
To test this API online, use:
- URI: http://emsapi.esy.es/rest
- Authorization:
123
or567
- username:
john
- password:
doe
How to cite this content
De Souza, Edson Melo (2021, March 13). Simple PHP API v.1.0.
Available in: https://github.com/EdsonMSouza/simple-php-api
Or BibTeX for LaTeX:
@misc{desouza2020phpapi,
author = {De Souza, Edson Melo},
title = {Simple PHP API v.1.0},
url = {https://github.com/EdsonMSouza/simple-php-api},
year = {2020},
month = {March}
}
License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.