All Projects → ManuelGil → REST-Api-with-Slim-PHP

ManuelGil / REST-Api-with-Slim-PHP

Licence: MIT license
REST API with PHP Slim Framework 3 and MySQL

Programming Languages

PHP
23972 projects - #3 most used programming language
TSQL
950 projects

Projects that are alternatives of or similar to REST-Api-with-Slim-PHP

grapevine
Fast, unopinionated, embeddable, minimalist web framework for .NET
Stars: ✭ 72 (+4.35%)
Mutual labels:  restful, restapi, restful-api, restful-webservices
slim-3-authentication
A Slim 3 authentication system.
Stars: ✭ 44 (-36.23%)
Mutual labels:  slim3, slim-framework, slim-3, slimframework
Restful Api Design References
RESTful API 设计参考文献列表,可帮助你更加彻底的了解REST风格的接口设计。
Stars: ✭ 4,830 (+6900%)
Mutual labels:  restful, restapi, restful-api
api rest slim framework
RESTFUL API o API REST con slim framework (PHP, MySql, PDO)
Stars: ✭ 14 (-79.71%)
Mutual labels:  slim, slim-framework, slim-3
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+2147.83%)
Mutual labels:  restful, restful-api, restful-webservices
slim3-mvc
Slim 3 PHP micro framework MVC application boilerplate
Stars: ✭ 24 (-65.22%)
Mutual labels:  slim-framework, slim-micro-framework, slimphp
Slim-Console
Slim Framework Console
Stars: ✭ 26 (-62.32%)
Mutual labels:  slim, slim-framework, slimphp
gorest
Go RESTful API starter kit with Gin, JWT, GORM (MySQL, PostgreSQL, SQLite), Redis, Mongo, 2FA, email verification, password recovery
Stars: ✭ 135 (+95.65%)
Mutual labels:  restful, restful-api
gothic
🦇 Gothic is a user registration and authentication SWT/JWT microservice. It supports REST, gRPC, and gRPC Web API, reCAPTCHA & a variety of DBs with Gorm.
Stars: ✭ 65 (-5.8%)
Mutual labels:  restful, restful-api
Slim-Auth
A Slim 4 Skeleton.
Stars: ✭ 22 (-68.12%)
Mutual labels:  slim, phpmailer
phpPgAdmin6
PHP7+ Based administration tool for PostgreSQL 9.3+
Stars: ✭ 45 (-34.78%)
Mutual labels:  composer, slim-framework
swagger-brake
Swagger contract checker for breaking API changes
Stars: ✭ 49 (-28.99%)
Mutual labels:  restful, restful-api
tinyspec
Simple syntax for describing REST APIs
Stars: ✭ 95 (+37.68%)
Mutual labels:  restful, restful-api
restful-services-in-pyramid
RESTful / HTTP services in Pyramid and Python course handout materials
Stars: ✭ 56 (-18.84%)
Mutual labels:  restful, restful-api
NodeRestApi
Node.js, Express.js and MongoDB REST API App
Stars: ✭ 38 (-44.93%)
Mutual labels:  restful, restful-api
HttpServerLite
TCP-based simple HTTP and HTTPS server, written in C#.
Stars: ✭ 44 (-36.23%)
Mutual labels:  restful, restful-api
angular6-httpclient-example
Angular 6 HttpClient: Consume RESTful API Example
Stars: ✭ 38 (-44.93%)
Mutual labels:  restful, restful-api
seal
django-base-templates 主要为 django 开发DEMO, 支持 非前后端分离 和 前后端分离模式 。
Stars: ✭ 118 (+71.01%)
Mutual labels:  restful, restful-api
akaash-rest-api
Akaash: A restful API template built with PHP driven by flight micro-framework
Stars: ✭ 17 (-75.36%)
Mutual labels:  composer, restful-api
slim-mobile-detect
Implements Mobile-Detect lib for Response's write on Slim Framework App
Stars: ✭ 18 (-73.91%)
Mutual labels:  slim-framework, slimphp

REST Api with Slim PHP

This API works with the same concept of social network of Fav Quote.

This is a simple REST Web Service which allow:

  • Post short text messages of no more than 120 characters
  • Bring a list with the latest published messages
  • Search for messages by your text
  • Delete a specific message by its id

🚥 Getting Started

This page will help you get started with this API.

Requirements

  • PHP 5.6
  • MySQL or MariaDB
  • Apache Server
  • Slim Framework v3

Installation

Copy this project

  1. Clone or Download this repository
  2. Unzip the archive if needed
  3. Copy the folder in the htdocs dir
  4. Start a Text Editor (Atom, Sublime, Visual Studio Code, Vim, etc)
  5. Add the project folder to the editor

Install the project

  1. Go to htdocs dir
  • Windows
$ cd /d C:\xampp\htdocs
  • Linux
$ cd /opt/lampp/htdocs
  • MAC
$ cd applications/mamp/htdocs
  1. Go to the project folder
$ cd REST-Api-with-Slim-PHP
  1. Install with composer
$ composer install

Or

$ sudo php composer.phar install

Create a database

Import the NETWORK SCHEMA DDL.sql file.

Import the NETWORK SCHEMA DML.sql file.

Or run the following SQL script

SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT, AUTOCOMMIT=0;
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

START TRANSACTION;

-- -----------------------------------------------------
-- Schema NETWORK
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `NETWORK` DEFAULT CHARACTER SET utf8 ;
USE `NETWORK` ;

-- -----------------------------------------------------
-- Table `NETWORK`.`COUNTRIES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NETWORK`.`COUNTRIES` (
  `ID_COUNTRY` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `ISO` VARCHAR(2) NOT NULL,
  `COUNTRY` VARCHAR(80) NOT NULL,
  PRIMARY KEY (`ID_COUNTRY`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Dumping data for table `NETWORK`.`COUNTRIES`
-- -----------------------------------------------------
INSERT INTO `NETWORK`.`COUNTRIES` (`ID_COUNTRY`, `ISO`, `COUNTRY`) VALUES
(1, 'AF', 'Afghanistan');

-- -----------------------------------------------------
-- Table `NETWORK`.`USERS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NETWORK`.`USERS` (
  `ID_USER` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `GUID` VARCHAR(20) NOT NULL,
  `TOKEN` VARCHAR(255) DEFAULT NULL,
  `USERNAME` VARCHAR(20) NOT NULL,
  `PASSWORD` VARCHAR(255) NOT NULL,
  `CREATED_AT` DATE NOT NULL,
  `STATUS` TINYINT(1) NOT NULL DEFAULT '0',
  `ID_COUNTRY` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`ID_USER`),
  UNIQUE INDEX `ID_USER_UNIQUE` (`ID_USER` ASC),
  UNIQUE INDEX `USER_UNIQUE` (`USERNAME` ASC),
  UNIQUE INDEX `GUID_UNIQUE` (`GUID` ASC),
  INDEX `fk_USERS_COUNTRIES1_idx` (`ID_COUNTRY` ASC),
  CONSTRAINT `fk_USERS_COUNTRIES1`
    FOREIGN KEY (`ID_COUNTRY`)
    REFERENCES `NETWORK`.`COUNTRIES` (`ID_COUNTRY`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Dumping data for table `NETWORK`.`USERS`
-- -----------------------------------------------------
INSERT INTO `users` (`ID_USER`, `GUID`, `TOKEN`, `USERNAME`, `PASSWORD`, `CREATED_AT`, `STATUS`, `ID_COUNTRY`) VALUES
(0, '5acff05a49592', NULL, 'ManuelGil', '', '2018-01-01', 1, 47),
(1, '5ba4524f296c3', NULL, 'testUser', '$2y$10$dRWUrwXE56p3zvEadmnMYeFivd6aU9BfGb4LXsmf5p.xQlkTAX/V6', '2018-01-01', 1, 1);

-- -----------------------------------------------------
-- Table `NETWORK`.`QUOTES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NETWORK`.`QUOTES` (
  `ID_QUOTE` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `QUOTE` VARCHAR(120) NOT NULL,
  `POST_DATE` DATE NOT NULL,
  `POST_TIME` TIME NOT NULL,
  `LIKES` INT UNSIGNED NOT NULL DEFAULT 0,
  `ID_USER` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`ID_QUOTE`),
  UNIQUE INDEX `ID_QUOTE_UNIQUE` (`ID_QUOTE` ASC),
  INDEX `fk_QUOTES_USERS_idx` (`ID_USER` ASC),
  CONSTRAINT `fk_QUOTES_USERS`
    FOREIGN KEY (`ID_USER`)
    REFERENCES `NETWORK`.`USERS` (`ID_USER`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Dumping data for table `NETWORK`.`QUOTES`
-- -----------------------------------------------------
INSERT INTO `NETWORK`.`QUOTES` (`ID_QUOTE`, `QUOTE`, `POST_DATE`, `POST_TIME`, `LIKES`, `ID_USER`) VALUES
(0, 'Fav Quote is a Micro Social Network with PHP, MySQL, Bootstrap 3 and Vue.JS 2. It don\'t use classes or a php framework.', '2018-01-01', '00:00:00', 1, 0);

-- -----------------------------------------------------
-- Table `NETWORK`.`LIKES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NETWORK`.`LIKES` (
  `ID_USER` INT UNSIGNED NOT NULL,
  `ID_QUOTE` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`ID_USER`, `ID_QUOTE`),
  INDEX `fk_LIKES_QUOTES1_idx` (`ID_QUOTE` ASC),
  CONSTRAINT `fk_LIKES_USERS1`
    FOREIGN KEY (`ID_USER`)
    REFERENCES `NETWORK`.`USERS` (`ID_USER`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_LIKES_QUOTES1`
    FOREIGN KEY (`ID_QUOTE`)
    REFERENCES `NETWORK`.`QUOTES` (`ID_QUOTE`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

COMMIT;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

Configure the project

Copy the .env.example file and call it .env.

Change the database configuration in the new file.

🎁 Donate!

If you want to help me to continue this project, you might donate via PayPal.

Donate via PayPal

📦 Deployment

Database Schema

Routes

  • get => /ping - This method is used for testing the api. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/ping

  • get => /login/{user}/{password} - This method gets a user into the database. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/login/testUser/testPwd

      parameters = [
        /** @var string $user - username */
        string	$user	=>	"testUser",
        /** @var string $password - password */
        string	$password	=>	"testPwd"
      ]
  • post => /register - This method sets a user into the database. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/register

      parameters = [
        /** @var string $user - username */
        string	$user	=>	"testUser",
        /** @var string $password - password */
        string	$password	=>	"testPwd",
        /** @var string $email - password */
        string	$email	=>	"[email protected]",
        /** @var int $country - country id */
        int	$country	=>	1
      ]
  • get => /validate/{user}/{token} - This method verify the user account. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/validate/testUser/326f0911657d94d0a48530058ca2a383

      parameters = [
        /** @var string $user - username */
        string	$user	=>	"testUser",
        /** @var string $token - token validation */
        string	$token	=>	"326f0911657d94d0a48530058ca2a383"
      ]
  • put => /update - This method sets a user into the database. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/update

      parameters = [
        /** @var int $country - country id */
        int	$country	=>	1
      ]
  • get => /verify - This method checks the token. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/verify

      headers = [
        /** @var string $authorization - JWT Authentication */
        string	$authorization	=>	"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJoZWFkZXIiOnsiaWQiOjEsInVzZXIiOiJ0ZXN0VXNlciJ9LCJwYXlsb2FkIjp7ImlhdCI6IjIwMTktMDEtMDEgMDA6MDA6MDAiLCJleHAiOiIyMDIwLTAxLTAxIDAwOjAwOjAwIn19.RTTPlUqE--WMP9M28-oj7p8MhWdisuuhWBsioDa_bgY"
      ]
  • post => /post - This method publish short text messages of no more than 120 characters. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/post

      parameters = [
        /** @var string $quote - quote */
        string	$quote	=>	"test",
        /** @var int $id - user id */
        int	$id	=>	1
      ]
  • get => /list - This method list the latest published messages. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/list

  • get => /likes/{id} - get method - This method list the users for likes. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/likes/1

      parameters = [
        /** @var int $id - quote id */
        int	$id	=>	1
      ]
  • get => /search/{quote} - get method - This method searches for messages by your text. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/search/quote

      parameters = [
        /** @var string $quote - text search */
        string	$quote	=>	"quote"
      ]
  • delete => /delete - delete method - This method deletes a specific message by its id. e.g.:

    uri = http://localhost/REST-Api-with-Slim-PHP/public/webresources/mobile_app/delete

      parameters = [
        /** @var int $id - quote id */
        int	$id	=>	1
      ]

💯 Running the tests

Use RestEasy or Postman app for testing.

For authentication you can generate a new JSON Web Token with the url login.

Put the parameters on a Query Parameter.

Put the token on an HTTP header called Authorization. e.g.:

  • Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJoZWFkZXIiOnsiaWQiOjEsInVzZXIiOiJ0ZXN0VXNlciJ9LCJwYXlsb2FkIjp7ImlhdCI6IjIwMTktMDEtMDEgMDA6MDA6MDAiLCJleHAiOiIyMDIwLTAxLTAxIDAwOjAwOjAwIn19.RTTPlUqE--WMP9M28-oj7p8MhWdisuuhWBsioDa_bgY
headers

Checks if the iat (issued at) and exp (expiration time) are correct in https://jwt.io/.

jwt

🔧 Built With

ℹ️ Changelog

1.0.0.8 (10/16/2019)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • Fix responses
    • Implements caches

1.0.0.7 (01/24/2019)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • New update-user route
    • Update send mail function
    • Update verify Authentication Token function

1.0.0.6 (01/19/2019)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • Setting up CORS

1.0.0.5 (09/23/2018)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • PHPMail integration
    • Protection of files with .htaccess
    • Improvement in documentation

1.0.0.4 (08/12/2018)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • TODO: Unit testing (Removed)

1.0.0.3 (07/07/2018)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • DotEnv integration

1.0.0.2 (03/29/2018)

  • Language: PHP
    Requirements:
    • PHP 5.6
    • MySQL or MariaDB
    • Apache Server
    Changes:
    • Add a new table in database to save likes
    • Add 3 methods (ping, register, likes)
    • Add logger with Monolog
    • Add JSON file for installation with composer

1.0.0.1 (12/07/2017)

👓 Authors

See also the list of contributors who participated in this project.

📝 License

This API is licensed under the MIT License - see the MIT License for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].