How to make Rest API using PHP/Mysqli Slim framework?

Hi viewers, Today i am going to teach, how to make REST api using php slim framework.

What is rest ApI?

REST stands for Representational State Transfer. (It is sometimes spelled “ReST“.).

This is a webservice. Which is used to provide the data to different kind of applications.This webservice will produce response in following formats.

1.XML

2.JSON

REST Api Using Slim framework Mysqli:

Let we start our api work.

1.libs – All the third party libraries and slim files will goes here.
2.include – database connection, database handler classes will be placed here.
3.api/index.php – Api request/response will be here
4..htaccess – Rules for url structure and other apache rules
5.documentation – Api documentation
6.Sql – Sql file placed here

Database
books database consists books table columns book_id, book_name, description

books.sql

 

1.Create .htaccess:

Create a folder and place following files inside folder

1..htaccess

2.index.php

I have created folder called api. and placed above files.

To remove index.php we are using here .htaccess. create a file named .htaccess and paste the following code inside

api/.htaccess:

2.Create database configuration and constant:

Create a folder called include. and place following code inside include. This file contains the entire project configuration like database connection parameters and other variables.

include/config.php:

Create another class named DbConnect.php This class file mainly takes care of database connection.

include/DbConnect.php

Now create another class named DbModels.php

This will handle CRUD. Creare, Read, Update, Delete

Paste this following code inside include

include/DbModels.php 

3.Handling Api Request/Response:

We have all required classes and db connection files for REST API.

We can start the request/response of book CruD Management .

We are going to perform following requests:

Api Url: http://localhost/your_project_mobi/api/

Create a file called index.php .add following code
api/index.php

The JSON response:

If you dont have any idea about JSON  Click here to go JSON Tutorial.

Every API request a JSON Response will return http Status Code. In Client side we have to verify http status code.

I am listing here some http Status Code:

100 => ‘100 Continue’,
101 => ‘101 Switching Protocols’,
//Successful 2xx
200 => ‘200 OK’,
201 => ‘201 Created’,
202 => ‘202 Accepted’,
203 => ‘203 Non-Authoritative Information’,
204 => ‘204 No Content’,
205 => ‘205 Reset Content’,
206 => ‘206 Partial Content’,
226 => ‘226 IM Used’,
//Redirection 3xx
300 => ‘300 Multiple Choices’,
301 => ‘301 Moved Permanently’,
302 => ‘302 Found’,
303 => ‘303 See Other’,
304 => ‘304 Not Modified’,
305 => ‘305 Use Proxy’,
306 => ‘306 (Unused)’,
307 => ‘307 Temporary Redirect’,
//Client Error 4xx
400 => ‘400 Bad Request’,
401 => ‘401 Unauthorized’,
402 => ‘402 Payment Required’,
403 => ‘403 Forbidden’,
404 => ‘404 Not Found’,
405 => ‘405 Method Not Allowed’,
406 => ‘406 Not Acceptable’,
407 => ‘407 Proxy Authentication Required’,
408 => ‘408 Request Timeout’,
409 => ‘409 Conflict’,
410 => ‘410 Gone’,
411 => ‘411 Length Required’,
412 => ‘412 Precondition Failed’,
413 => ‘413 Request Entity Too Large’,
414 => ‘414 Request-URI Too Long’,
415 => ‘415 Unsupported Media Type’,
416 => ‘416 Requested Range Not Satisfiable’,
417 => ‘417 Expectation Failed’,
418 => ‘418 I\’m a teapot’,
422 => ‘422 Unprocessable Entity’,
423 => ‘423 Locked’,
426 => ‘426 Upgrade Required’,
428 => ‘428 Precondition Required’,
429 => ‘429 Too Many Requests’,
431 => ‘431 Request Header Fields Too Large’,
//Server Error 5xx
500 => ‘500 Internal Server Error’,
501 => ‘501 Not Implemented’,
502 => ‘502 Bad Gateway’,
503 => ‘503 Service Unavailable’,
504 => ‘504 Gateway Timeout’,
505 => ‘505 HTTP Version Not Supported’,
506 => ‘506 Variant Also Negotiates’,
510 => ‘510 Not Extended’,
511 => ‘511 Network Authentication Required’

CRUD:

C:Create:

Add Books using following parameters.

ADD BOOKS:

  • URL : /add_books
  • Method : POST
  • Header : NA
  • Parameters:
  • book_name
  • description

Add this code in index.php . It will handle add books

index.php

In the following table you can find the API request information about the URL, HTTP method and the parameters needed to be posted.

URL /add_books
Method POST
Parameters book_name, description

Successful Json response:

{
“error”: false,
“output”: “Record added Successfully”
}

If Parameters missing following json will be issued.

{
“error”: true,
“message”: {
“book_name”: “Book Name is required”
}
}

{

“error”: true,
“message”: {
“description”: “Description is required”
}
}

R:Read

Get ALL BOOKS:

  • URL : /get_books
  • Method : GET
  • Header : NA

Parameters :NA

Add this code to get list of books

URL /get_books
Method GET
Parameters N/A

successful Response:

Failure Response: If data are not exists

U:Update

EDIT BOOKS:

  • URL : /update_books
  • Method : POST
  • Header : NA
  • Parameters:
  • book_id
  • book_name
  • description

To edit the book details use following code in index.php

Success Response:

{
“error”: false,
“output”: “Record updated Successfully”
}

Failure Response:

{
“error”: true,
“message”: “Failed to Update”
}

D:Delete

DELETE BOOKS:

  • URL : /delete_books
  • Method : POST
  • Header : NA
  • Parameters:
  • id

To delete a book details, use following code in index.php

Success Response:

{
“error”: false,
“message”: “Record Deleted Successfully”
}

Failure response:

{
“error”: true,
“message”: “Invalid Request”
}

Testing Api in Rest Client:

To test api we should use Rest Clients. We are going to test it on POSTMAN rest client.

 

URL Method Parameters Description
http://localhost/prabaweb/api/add_books POST book_name, description Add Book
http://localhost/prabaweb/api/update_books POST book_name, description, id Update Book
http://localhost/prabaweb/api/delete_books_by_id POST id Delete Book
http://localhost/prabaweb/api/get_books GET id Fetching all Books

REST API Testing

I gave demo video here ,  to test api requests

Video tutorial coming Soon…

Download Project Code

[purchase_link id=”515″ style=”button” color=”red” text=”Download Project Code” direct=”true”]

Updated: June 3, 2017 — 10:56 pm

Leave a Reply

Your email address will not be published.

prabaweb.in Codelab- programming blog © 2017