PHP Classes

PHP API Tutorial Training Server: Implementation of a REST API for learning purposes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 136 All time: 9,277 This week: 55Up
Version License PHP version Categories
training-server 1.0.0MIT/X Consortium ...5HTTP, PHP 5, Libraries, Web services
Description 

Author

This package provides an implementation of a REST API for learning purposes.

It provides all the files to implement an API using REST HTTP requests, including user authentication using OAuth.

The package provides an API example implementation endpoints to demonstrate features like registered user access, setting cookies in the API responses, and retrieving file data as responses.

It also provides files to set up a Docker container to test the provided API.

Innovation Award
PHP Programming Innovation award nominee
September 2021
Number 3
Creating an API can be a complex process. Depending on what you want the API to do, you may need to implement other features besides the actual API function.

For instance, you may need to implement API user management and customize the API responses to address the API user's needs better.

This package provides a solution to allow you to train yourself in API creation.

You can set up a Docker container to create a closed environment specifically to test the APIs you will make.

Manuel Lemos
Picture of Kamil
  Performance   Level  
Innovation award
Innovation award
Nominee: 1x

 

Example

<?php

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::get('basic-auth', function(){
    return
response()
        ->
json([
           
'status' => 'ok',
        ]);
})->
middleware('auth.basic.once');


Route::get('plain/users', function() {
    return
response()
        ->
json(\DB::table('users')->select()->limit(20)->get());
});

Route::get('plain/cookies', function() {
    return
response('ok')
        ->
cookie('test', 'value', 10)
        ->
cookie('another', 'cookie', 10);
});

Route::get('oauth2/users', function(){

    return
response()
        ->
json(\DB::table('users')->select()->limit(20)->get());

})->
middleware('auth:api');

Route::get('plain/files/{type}', function($type) {

   
$types = [
       
'csv' => 'text/csv',
       
'css' => 'text/css',
       
'js' => 'text/javascript',
       
'png' => 'image/png',
    ];

    if(!isset(
$types[$type])) {
        throw new \
Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
    }

    return
response()->download(
       
base_path() . '/resources/assets/sample-files/sample.' . $type,
       
'sample.' . $type,
        [
           
'Content-Type' => array_get($types, $type),
        ]
    );
});


Details

Training server for RestControl

TrainingServer contains several REST endpoints e.q. with oAuth/basic authentication, which you can use for learning RestControl.

Installation

 make build
 make first-run

Start server

 make start

Usage

 # Run all tests
 make run-tests

Endpoints

-> Plain api(without any authorization)

(GET) http://IP_ADDR/plain/users

Sample response:

[
  [
      "id": 1,
      "name": "Sample name",
      "email": "sample@email.com",
      "password": "sample_long_password_string",
      "remember_token": null,
      "created_at": "2018-01-01 10:00:00",
      "updated_at": "2018-01-01 10:00:00",
  ],
  [
      "id": 2,
      "name": "Sample name",
      "email": "another@email.com",
      "password": "sample_long_password_string",
      "remember_token": null,
      "created_at": "2018-01-01 10:00:00",
      "updated_at": "2018-01-01 10:00:00",
  ],
]

(GET) http://ID_ADDR/plain/files/csv (GET) http://ID_ADDR/plain/files/css (GET) http://ID_ADDR/plain/files/js (GET) http://ID_ADDR/plain/files/png (GET) http://ID_ADDR/plain/cookies

-> Basic auth

(GET) http://IP_ADDR/basi-auth

Sample response:

 [
    "status": "ok"
 ]

-> oAuth2 Endpoints

(POST) http://IP_ADDR/oauth2/token

> Form params: > - grant_type(optional): password, client_credentials > - client_id: string > - client_secret: string > - username: string > - password: string > - scope(optional): string > > Seeds: > - Password grant client > - client_id: 10 > - client_secret: F0NVue12qNwayx3pKJLHfJmQouOZg40YZafjjdHZ

(GET) http://ID_ADDR/oauth2/users - Return list of users.

>Headers: > - Content-Type: application/json > - Authorization: Bearer ACCESS_TOKEN > >

Sample response:

[
  [
      "id": 1,
      "name": "Sample name",
      "email": "sample@email.com",
      "password": "sample_long_password_string",
      "remember_token": null,
      "created_at": "2018-01-01 10:00:00",
      "updated_at": "2018-01-01 10:00:00",
  ],
  [
      "id": 2,
      "name": "Sample name",
      "email": "another@email.com",
      "password": "sample_long_password_string",
      "remember_token": null,
      "created_at": "2018-01-01 10:00:00",
      "updated_at": "2018-01-01 10:00:00",
  ],
]

  Files folder image Files (87)  
File Role Description
Files folder imageDocker (5 files)
Files folder imageserver (9 files, 7 directories)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:136
This week:0
All time:9,277
This week:55Up