How To Create A REST API In Codeigniter With Basic Authentication

How To Create A REST API In Codeigniter With Basic Authentication
Codeigniter is a well known framework for PHP application development. However, in the cases where the application needs to communicate across platforms, you do need a RESTful API. In almost all cases, REST API is an essential component of web apps.
rest api codeigniter

What is REST API

REST stands for Representational State Transfer.  A REST API is a web service which uses HTTP methods likes GET, PUT, POST, DELETE for data manipulation over the cross platforms.
In this tutorial, I will demonstrate How you can create a REST API in Codeigniter. To create the API, I will use codeigniter-restserver, written by Phil Sturgeon and currently supported by Chris Kacerguis. I will also use the codeigniter-restclient library.
The process of creating REST API in Codeigniter covers the following steps:
  • Installation of Codeigniter framework on Cloudways
  • Database and table(s) creation
  • Setup libraries and permissions
  • Setup authentication and API key(s)
  • Setup HTTP calls (GET, PUT, POST, DELETE)
  • Test the HTTP calls.

Installation of Codeigniter on Cloudways

First sign up at Cloudways for a free account. Once the account is ready, login to your account and create a new server. Fill in the server and the application detail and select PHP Stack as your application. Next, enter application, server and project’s name.
Note: You can host unlimited applications on a single server.
Choose your provider (Google, Amazon, Vultr, DigitalOcean, Kyup), select server size according to your needs and click the Launch button. Check out the following GIF for more details:
Now that your server and application is ready, open your server by clicking the server name.
Login with the username and password provided in the Master Credentials area.
Now that you are connected to your server, go to the SSH terminal and type the following commands to install Codeigniter.
Once the download of the zip file finishes, unzip the file by using the following commands.
At this point, the installation is complete.
Go to the Application tab on the Cloudways panel and select your application. Click the highlighted button (see the following image) to access the application. Remember to add /codeigniter to the URL and hit the Enter key.

Create Database and Table(s)

I will now create a simple database with a table named User. In order to create the database, go to the Application Management tab and launch the database manager.
Type in the following command in the SQL command field:

Setting up Libraries and Permissions

First of all, download codeigniter-restserver and codeigniter-restclient libraries. Extract the contents and then drag and drop application/libraries/Format.php and application/libraries/REST_Controller.php files into the application’s directories.Remember to add require_once it at the top of the controllers in order to load them into the scope. Additionally, copy rest.php file from application/config in application’s configuration directory.
Now create a file in the application’s root folder and name it .htaccess. Paste the following code in it.

Setup Authentication and API Key

To setup authentication, first create the following tables in the database:

The table Keys will be used for storing the API key, and the Logs table will hold the logs of the request(s) received by the server.
Now open up application / database.php and type in your hostname, dbname and password (available in the Application Access details).
The next step is the setup of authentication. For this, open up application / autoload.php and change this line of code
To this
Now go to application / rest.php and set the following entities as shown
The authentication is now ready. Nest up is the creation of the model and HTTP calls.

Setup HTTP Calls

I will now create two files.
Go to application/controllers and create a new file with the name of api.php. Paste the following code in it.
Next, go to application/models and paste the following code in it.

Testing the HTTP Calls

To test the HTTP calls of the API, I will use Postman.Go to the Postman, Set the method to GET , then set the authentication and API key as shown below:
Now to test the POST request, set the request to POST and add the authentication and API key. Fill in the variables as shown below:
Next, I will test the PUT request. Pass the id in the 3rd segment of the URL, set the request to PUT, set the authentication and the API key and fill in the parameters as shown below:
To test the DELETE request, pass the id in the 3rd segment of the URL, set the request to DELETE, set the authentication and the API key and fill in the parameters as shown below:

To Sum Up

In this tutorial. I described how you could setup authentication for a REST API in Codeigniter. I created four API calls for data manipulation.
If you need any help with the code or the idea of implementing your own RESTful API in Codeigniter, do leave a comment below.