Before starting REST API, I would like to explain what exactly an API is.
API is an abbreviation for Application Programming Interface. API is a set of instructions ( functions and routines) that accomplish a specific task or are allowed to interact with a specific software component like database or computer hardware.
Why API?
API helps you to perform CRUD (Create, Read, Update and Delete) operation on a particular website from remote place.
Let’s say..
You have a website where you have applied opt-in form to capture leads of the users. Any user who register on your website, data will be stored on remote website.
In same fashion..
You can delete stored entries on remote website via API. In real API allow you to access remote website for all the necessary operations.
Here is an example of MailGet API that helps you to bulk send email and manage your contacts. MailGet is best for sending bulk emails. Take a look how API works.
Standards-based, i.e, runs on top of HTTP protocol, and
It can easily be used in the presence of firewalls.
HTTP Methods v/s CRUD Operations:
Now, let’s take a look at how HTTP methods can be mapped to CRUD operations.
HTTP Method
CRUD Operation
GET
READ
POST
INSERT / CREATE
PUT
UPDATE
DELETE
DELETE
Files To Be Needed:
We’re going to use Phil Sturgeon’s Rest Server and Rest Client. We’ll use CodeIgniter framework of PHP to develop Rest API.
These are the library files for our respective server and client files.
We need to include these files inside the library folder of the application folder of our project.
Process:
First of all, we need to understand the process of REST API. Let’s take a look at it.
As we know that it’s a client-server model, we need to create 2 files. One will work at the client end and other at the server end. These 2 files plays a very important role in REST architecture.
Client File:
All requests pass through this file. This file handles every type of request and calls the respective method and pass the data to the server.I’ll explain this in detail later. In this file, we have to mention the server address, login credentials with API key so that it will authenticate the user. Along with credentials, HTTP methods ( GET, POST, PUT, DELETE ) are defined. Use of these methods are just to collect data from the user from the front end and transfer it to the server using HTTP, CURL etc. We’re going to use CURL.
Server File:
The server receives the request and performs the respective operation. This file interacts with both the client file and database directly. So ultimately this file is going to handle everything behind the scenes.
Now, I’ll explain each file one by one.
Controller : Client.php ( Client File )
In client file, I’ve passed parameters required for authentication and connection with the server to the client library. Client library then connects to the server using curl. Authentication details, server details, and other HTTP headers are passed using curl.
Once authentication has been done, the respective method will be called in the client file. Say, if we’re using get method. Then get method of client file will be called.
It further call another get method inside it, along with parameters (URL, Data, and Format) whose definition is in the rest library.
It then passed to _call method which resides in the rest library and is going to pass data to server file through curl.
Controller : Api.php ( Server File )
In server file, first thing that is going to call is the constructor of the server file. As we’ve inherited REST_controller class, constructor of REST_controller will also get called and it will call appropriate functions for various purposes (like detect_method, authentication, parsing get method to data_get method, etc.) and then calls the data_get method in server file.
In this data_get method database operations will take place. As we are using CodeIgniter, all the queries are written in the model file, i.e., API_model in our case.
Model : API_model.php
Htaccess File
Apart from this process, you may need to edit your htaccess file present in your application folder.
Following line meant that all the requests coming from the client must be forwarded to api.php, i.e., our server file.
So, this is the complete process for implementing Rest API. You may need to make changes in the library files. You can also create your own authentication method/algorithm if you’d like to.
Database
First of all create a database with a name ‘books’ with the following query :
Now, create a table inside this database with the following query :
Procedure To Run This Project –
You can easily run this project with following url’s for respective operation.
For each operation you need to edit the type of method in the read.php file.
For example : put in this case and method will be post.
For Put & Post, Method will be post & for Get & Delete, Method will be get.
For Read Operation :
You may or may not need to mention a particular id you want to fetch in the end of the URL like this :
For Create/Insert Operation :
You need to insert data in the form.
For Update Operation :
You need to insert data you want to update in the form along with the id at which you want to update this information.
For Delete Operation :
You may or may not need to mention a particular id you want to fetch in the end of the URL like this :
a simple restful api using php & codeignitercodeigniter 3 rest apicodeigniter web servicescodeigniter rest api authenticationcodeigniter rest_controllercodeigniter rest server api keycodeigniter rest server examplecodeigniter rest api jsoncodeigniter web servicesa simple restful api using php & codeignitercodeigniter rest_controllercodeigniter rest api authenticationcodeigniter rest server examplecodeigniter rest clientcodeigniter rest client examplecodeigniter rest server api keyphp tutorial for beginnerstutorial phpphp websitelearn php onlinephp programsphp programming languagephp online coursephp mysql tutorialphp examplephp trainingphp source codephp 7 tutorialtutorialspoint phpphp urlscript phpadvanced php tutorialphp web developmentphp javascriptphp ajax tutorialphp lernenphp code generatorphp for dummiesphp developeronline php classesonline php coursephp developer coursephp development tutorials php tutorial for beginners pdfphp programming languagephp server scriptphp website projectphpmyadmin sqlprograming phpphp programming coursesphp training coursephp tutorial classesphp tutorial for beginners pdftutorial php classphp tutorial for beginners with examples pdfphp tutorial pointphp interview questionsphp pdfphp basicsphp syntaxphp tutorial for beginnersphp tutorial pdfsphp tutorial