In this tutorial I am going to show you how we can work with RESTful webservice or REST webservice in PHP PDO, MySQL.

Introduction

The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).
Methods

HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.

Step 1. Create below MySQL table

Step 2. Create below Db class under directory <project root directory>/config/Db.php

Please do not forget to change the database credentials as per your database. The above class will return connection object for the MySQL database.

Step 3. Create below Department class under <project root directory>/object/Department.php

The above class represents the department object for department table.

Step 4. Now we will read all departments from the database. So create below file under <project root directory>/department/read.php

In the above file you see, we have called read() method of department object, so let’s create read() method inside Department class.

Step 5. Add below read() method to Department.php

When you hit the URL GET http://localhost/<project root directory>/read.php in REST client you see below output in the browser

Step 6. Now we will create a new department to the database. So create below file under <project root directory>/department/create.php

Notice in the above file while we are creating a new department we are not initializing department id to the department object because id will be created automatically in the database.

Step 7. Add below create() method to Department.php

When you hit the URL POST http://localhost/<project root directory>/create.php in REST client using below body param, you see below output in the browser

Body

Output

message    "Department was created."

Please check the table to ensure that department has been created for the given department name.

Step 8. Now we will update department to the database. So create below file under <project root directory>/department/update.php

Step 9. Add below update() method to Department.php

When you hit the URL PUT http://localhost/<project root directory>/update.php in REST client using the below body parame, you see below output in the browser

Body

Output

message    "Department was updated."

Please check the table to ensure that department has been updated for the given department id and name.

Step 10. Now we will update department to the database. So create below file under <project root directory>/department/delete.php

Step 11. Add below delete() method to Department.php

When you hit the URL DELETE http://localhost/<project root directory>/delete.php?id=45 in REST client you see below output in the browser

message    "Department was deleted."

Please check the table to ensure that department has been deleted for the given department id.

Thanks for reading

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.