CodeIgniter Tutorial How To Make Rest API ( Server And Client )

This CodeIgniter tutorial will discuss how to create REST API from both server and client side. CodeIgniter has several libraries that support and easy to create REST APIs application. Currently, REST API becomes a communication method that must be used for web-based applications. CodeIgniter as the simplest PHP framework to create REST API. Consider the following tutorial

REST (REpresentational State Transfer) is a communication method architecture that is often applied in the development of web-based services. REST API is run through the HTTP / HTTPS protocol. REST APIs involve the process of reading certain web pages that generate JSON format. After going through a certain definition process, consumers can access the data presented by the server side.

Read another article: Rest API And HTTP Methods (GET, POST, PUT, DELETE) Using Slim Framework And PHP

The uniqueness of REST is the communication between client and server facilitated by some unique operational types (verbs) and Universal Resource Identifiers (URIs) for each resource. Each verb (GET, POST, PUT, DELETE) has special operational meaning to avoid ambiguity. Okay, we started the CodeIgniter tutorial how to create a REST API with a simple example.

# CodeIgniter Tutorial Creating a Simple REST API Server

Download and Install CodeIgniter

We will skip this section because I’ve covered in the complete CodeIgniter tutorial in the article “Simple CRUD Apps With Codeigniter Tutorial For Beginners.” Please change your project folder as “codeigniterapi” folder.

Creating Database And Table

The database and tables used are still the same as the previous article “pos” database and “m_item” table. To shorten the time, please create a new MySQL database with the name “pos” and copy the following script to create “m_item” table and fill the dummy data.

Installing the CodeIgniter Restserver Library

  • Please download the library codeigniter-restserver
  • Copy the “application/libraries/Format.php” and “application/libraries/REST_Controller.php” files into your “application/libraries/” folder. Copy the rest.php from “application/config/rest.php” and paste in your application’s configuration folder.
  • Copy the language’s folder into application’s folder

Create a web service to handle request data from “m_item” table.

To make the request handling using the codeigniter-restserver library is quite easy, we just need to add an “index_” prefix to the function for each request header. The basic use of the following code:

Create a controller named Api.php which is the resource of the web service that will handle the data management request from the m_item table. Please copy the following script:

Testing REST API CodeIgniter using Postman Plugin

To test the script we have created above, you can use POSTMAN plugin on chrome browser or Firefox. In my tutorial using chrome browser, Please install POSTMAN.

The first test is to make a request to the server to display all data from “m_item” table with GET method like the following picture

Codeigniter Tutorial Create Rest Api With Get Header URI

The second test is to request on the server to save new data to “m_item” table with POST method. Please follow the following picture

Codeigniter Tutorial How To Create Restfull Api For Beginner

The third test is to request to the server to update the data table “m_item” with PUT method as shown below

Send PUT Header To Rest Api Codeigniter Tutorial

The fourth test is to request on the server to delete data on “m_item” table with DELETE method as shown below

Codeigniter Tutorial How Send DELETE Header To Rest Api Server With Postman

Until this step, CodeIgniter Tutorial to create a simple API server has been completed; the next step will explain how to access the API from the client side

# CodeIgniter Tutorial Creating a Simple Rest Client using CURL

To be able to access the server API that we created earlier. Surely we must learn to create a Rest Client to access the API. Here’s the tutorial

Note: for the REST Client project is continuing the example project previously made in the article “Simple CRUD Apps With Codeigniter Tutorial For Beginners” please download project first.

1. Create a new CodeIgniter library to access the API server with the name Restclient.php in the folder “mycodeigniter/application/libraries/Restclient.php”. Copy the following script:

2. Modify the model on product_model.php

3. Change the Product.php controller

Test these script in your browser  http://localhost/mycodeigniter/index.php/product, and the result must be like the following picture

Codeigniter Tutorial How To Create Rest Api Client

Until this step, CodeIgniter Tutorial REST API SERVER And CLIENT is done. You can download the entire project from the download link below

 

 

Thus my article about CodeIgniter Tutorial How To Make Rest API ( Server And Client ) hope useful 🙂

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.