A REST API is a web service which uses HTTP methods likes GET, PUT, POST, DELETE for data manipulation over the cross platforms. We will build a PHP Rest Api Using codeignitor.
Question- What is API?
Answer - API Stands for Application Programming interface (to know more about Click here )
Question - What is REST in REST API?
Answer - REST stands for Representational State Transfer.
You can download the codeignitor from here - https://codeigniter.com/download
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:
A REST API is a web service which uses HTTP methods likes GET, PUT, POST, DELETE for data manipulation over the cross platforms. We will build a PHP Rest Api Using codeignitor.
Question- What is API?
Answer - API Stands for Application Programming interface (to know more about Click here )
Question - What is REST in REST API?
Answer - REST stands for Representational State Transfer.
You can download the codeignitor from here - https://codeigniter.com/download
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:
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.
You might be interested in: How To Pass Data From Controller To View In CodeIgniter
Now create a file in the application’s root folder and name it. Paste the following code in it.
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.
You might be interested in: How To Pass Data From Controller To View In CodeIgniter
Now create a file in the application’s root folder and name it .htaccess. Paste the following code in it.
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

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.
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.
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:

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.
Source - https://www.cloudways.com/blog/rest-api-in-codeigniter/