A Quick support to compose Web services in PHP Development


Where there is technology, there comes the dynamism. Every day we can see new websites or mobile apps that use Web Services. Web services in PHP Development Services are used to process the request coming from mobile API and send the response in JSON format back to the mobile API.

Types of web services:

There are commonly two types of web services:

1) SOAP Web services

2) REST Web services

  1. SOAP Web services:

SOAP (Simple Object Access Protocol) is XML-based web service protocol.  SOAP uses XML for accepting the request and returning response. SOAP defines its security protocols also called as WS security.

As SOAP uses only XML format to be used and sets many standards that need to be followed strictly, so the application using SOAP becomes slow. Also, SOAP consumes more bandwidth.

  1. REST Web services:

    REST stands for REpresentational State Transfer. REST uses JSON, XML, HTML, Plain text, etc. formats for accepting requests and returning response.

REST does not define many standards as that of SOAP and consumes less bandwidth

The Difference between SOAP and REST:

SOAP REST
SOAP uses only XML format for requests and responses. REST uses XML, JSON, HTML etc. formats for requests and responses.
SOAP defines its own security protocols. Instead of defining own security protocols, REST rather inherits the security measures.
SOAP requires more bandwidth. REST requires less bandwidth

 

REST Web services in PHP:

Fig.1(a)

Web services that are based on REST are also called as RESTful web services.

As shown in figure 1(a), the data/request coming from any mobile API is accepted in PHP using POST method.  Based on the action to be performed, the request is processed and the response is send back to API in the JSON format.

For example, if you are a backend developer and are writing a web service for a mobile API implementing Login functionality, then the request for web service would be Username/Email Id and Password.

Chrome extension for Advanced Rest Client

For testing your web service, Chrome provides Rest client extensions like Postman Rest Client.

It can be easily added from Chrome webstore.

As shown in the figure 1(b), you can enter therequest URL of your web service like, http://localhost/your_path_to _webservice /login.php

Fig.1(b)

In key and value, you need to set the parameters for that web service. Like in login webservice, you need parameters as’ username’ and ‘password’.

You need to accept this request parameters in your login.php file in POST method.

After validating the credentials from database, send the response in the JSON format.

For this you need to encode the result in JSON format using json_encode () function.

The response in case of successful login will be in the format like following:

{

    “status”: “Success”,”message”:”Log in Successful”

}

In case of failure, the response will be like:

{

    “status”: “Failed” , ”message” : ”Login failed”

}

Conclusion:

Web services are one of the most important part in today’s application development.They are easy to understand and we can easily create a web service of our website without needing to hire backend developer.

Share this: