Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

REST API IN PHP

What is API ?

API or better known as Application Programming Interface is an application which provides a method by which we can exchange data between two applications.One of these application serve as a server and the other one serve as a client.

Now let us understand this by a simple example.Let’s say you have a database and you want to provide data to Mobile Application and a website.Now how will the mobile app and website both will communicate with the database?

We donot want to communicate directly with the database.So in this case we need API calls which can act as a bridge between database and both mobile application and website.

These API calls will communicate with database and provide data to mobile application and as well as website.

Mobile application and website acts here acts as a client whereas on the other hand we need to create API calls which will act as server.

Client will make request to server.Server will process and communicate with database and in result will give response in a standarized format to client.

The response returned can be in any format but the most prefered return type is JSON format.By using this return format it can be used by almost any type of application ie whether it is a mobile application developed using Java or a simple website created using PHP.

How API Works

When a client like mobile app make some requests to these API calls ,then those requests are sent to server.they are processed there and then the response with status,data and message are returned in a standard format type.This response is then consumed by client side and the appropriate data is shown in mobile app.

What are types of API?

API are of two types one is SOAP and other one is REST.We are here going to dscuss the REST API.

REST is the representational state transfer which is an architectural style to exchange data between two applications.

REST API is stateless means one call is independent of another call and each call contains the appropriate data to complete the call.

REST is not constrained to any format.Response can be returned in any format likeXML,JSON,YAML.It follows HTTP protocols and it is incredible because of it’s flexibility.

It must follow below principles while getting implemented:

1.Uniform interface

2.Client server

3.stateless

4.Cacheable

5.Layered system

6.Code on demand(optional)

Well what is a resource?

Resource can be any information which is searched or which can be manipulated. REST uses Resource identifier to identify a particular resource.

There are several methods which are used to create or manipulate resources.We can perform a desired action using these methods.These are the HTTP methods like GET/POST/PUT/DELETE . We can perform CRUD operations using these methods.These methods should be used uniformly ie if you have used POST to update a resource then this approach should be used for other resources too throughout the application.

Generally the methods are used as follow:

GET: GET is used to fetch a resource.

POST:POST is used to create a resource.

PUT :PUT is used to update a resource.

DELETE:DELETE is used to delte a resource.



This post first appeared on PhpGuruAcademy, please read the originial post: here

Share the post

REST API IN PHP

×

Subscribe to Phpguruacademy

Get updates delivered right to your inbox!

Thank you for your subscription

×