Web Service in PHP Example

47

Representational State Transfer (REST) is an architecture style. Web services that follow the RESTful principles are RESTful services. In RESTful services, URIs are used to access the resources. Data and functions are called resources in the RESTful glossary. So eventually data and function are what we will access via services.

In this RESTful web services tutorial, I will show you how to create a RESTful web service. I will not be using any framework for this, just plain PHP. Most of the times I do prefer to write custom code without depending on frameworks, this approach has its own advantages.

On the Internet, I have seen web services tutorials and most of the times they all turn out to be error-prone or incomplete. I tested those RESTful services using a REST client and mostly they fail.

Objectives of this RESTful Web Service Example

  • Build a RESTful Webservice.
  • With plain PHP, without dependency on any framework.
  • URI patterns should follow REST principles.
  • RESTful service should be capable responding to requests for formats like JSON, XML.
  • Should demonstrate the use of HTTP Status code based on different scenarios.
  • Demonstrate the use of Request headers.
  • Test the RESTful web service using a REST client.

RESTful Webservice Example

Following is a domain class used to demonstrate the RESTful services.

RESTful Services URI Mapping

RESTful web services should have a uniform, neat URIs. HTACCESS file is used to do the URL mapping between the request and the real file. In this example, we are using two URIs.

To get the list of all mobiles.

To get a particular mobile’s detail using its id. In the below ‘2’ is the id of a mobile.

Following is the file that.htaccess  maps the request URL and forwards the request to a PHP file.

RESTful Web Service Controller

In file.htaccess, we are forwarding all the request to file RestController.php with a key named ‘view’ to identify the request. Following is the RestController.php file that receives the request dispatches to respective methods to handle the request. ‘view’ key is used to identify the URL request.

A simple RESTful base class

Following class has a couple of methods that can be commonly used in all REStful service handlers. One method is used to construct the response and another method is to hold the different HTTP status code and its respective messages. Such common methods can be added to this class and this can be made a base class for all RESTful handler classes.

RESTful Web Service Handler

This is the class that handles the REST request. You need to note a couple of things carefully. First one is how the REST handler decides what kind of response format should be sent back. This is decided based on the Request Header parameter “Accept”. The protocol here is, when the request is sent, it should set the Request header parameter “Accept” and send it. The values can be like “application/json” or “application/xml” or “text/html”. The second thing you should note is the usage of Status codes. For success, status code 200 should be set in response and sent. Similarly, there are different status codes available and they should be used according to the situation.

RESTful Web Service Client

To test a RESTful web service, you have to write REST client programmatically and consume the service. Another way is to use a REST client. There are many standalone REST clients available in the market. I generally use a Google Chrome plugin REST client. “Advance Rest Client” is a Google Chrome extension and it is a good REST client. You need to add the extension and use it as below. Following are the screen shots of testing the above example RESTful web service.

Leave A Reply

Your email address will not be published.