Create web service with PHP and MySQL

Have you ever heard about the Web Service (web service) ? In this article, I will guide you how to make a web service to serve the requirements of the client easily and simply! First, you need to understand what web service is, right?

tao-web-service-elephant-php-va-mysql

WHAT IS WEB SERVICE?

A Web service is a collection that provides methods that allow remote calls via an HTTP URL or another connection method. The result it returns is usually in the form of XML or Json. Because of the remote remote call, it is applied to build distributed systems and applications.

SOME FEATURES OF THE WEB SERVICE

  • Can be accessed by any application
  • Regardless of the programming language
  • Support manipulation between heterogeneous components
  • Easy maintenance and low development costs

CREATE WEB SERVICE WITH PHP LANGUAGE AND MYSQL database

In this section, I will guide you to create a simple web service that provides the entire list of students in a returned school in XML or Json format.

Step 1: Create any database then create a data table consisting of fields such as: id, fullName, class. Then insert some test records. You can run the following SQL command to create it immediately.

Step 2: Create a php file located on the web server so that we can call from a specified URL later. You create the webservice.php file with the source code as follows:

The above code takes the parameters passed on the URL as a format to determine the type of data to return:

  • URL gets information in the form of Json:

  • URL retrieves information in XML format:

With the above parameters, the web service will execute to retrieve all student data and return the data. For example, taking XML will have the following data set:

Step 3: Create the client.php file with the source code content as follows:

SUMMARY

From the above example, you just need to understand simply that webservice is only a service where it receives requests via URL and parameters, then it will handle returning data in xml format or json that you desire at the request of the service user.

Building webservice for the application is very useful because the json or xml format is a data format that can be used on many mobile platforms such as android, ios, … or websites like PHP, JSP, ASP.net, C # , ….

ITZone via kienthucweb.net

Share the news now