Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

ManuelGil/REST-Api-with-Slim-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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	=>	"example@example.com",
        /** @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

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.