Limited time offer Get 30% OFF, Discount code: SAVE30
818-966-0495 Order Now
PHP Web Developments
01 Aug

How to Use PHP for Developing a Web Service?

October 26, 2017

To begin with, we would like to clear the air about the concept of web services. Irrespective of all that has been said and done in this regard, in the simplest form, a web service remains to be a combination of DLL files which are compiled and uploaded over the internet, and can be accessed by the users in general, regardless of the language that their system is using.

While working on a C# code, all we have to do is to simply type [webservice] and .Net would do the rest of the work for us. However, in this article, we have much more focused approach, and shall factor in PHP development services, simultaneously trying to unravel and understand and appreciate the actual functioning of the backend development. Stay with us, if you would like to understand how web services can be created using PHP.

How does the web service actually works?

There are two concepts in particular that we need to be familiar with, in order to understand the procedure. These two concepts are:

1 – WSDL – WSDL or the Web Service Definition Language enables the developers to express what the web service is going to serve to the users. In this, the developers actually get to define all the functions that they are going to use in the development process.

2 – SOAP – Let us try and learn this concept with the help of an example of an eating joint. Now, you go to an eating joint and browses through the list from the items present in the menu. You let your order be known to the waiter. It is then the responsibility of the waiter to take your order to the culinary department and bring the order back to you on your desks. Where WSDL was the menu – containing the list of all the functions possible, SOAP is our ardent hard working Waiter. It takes the requests of the users via various input methods, matches it with the specifications available, and reverts with the required set of functions.

What all do you require?

To come up with a thorough web service, you would need to have certain things right in place, the list of which includes PHP Soap. Below we shall discuss various ways to enable the same:

  • While using PHP 5 or a higher version on Windows, in order to install the Soap, all you have to do is visit the php.ini section and uncomment if the following extension prevails: extension=php_soap.dll. Once through, you would be able to see the installation when running the PHPINFO.
  • Linux users can achieve the same by the following code – yum install php-soap
  • PHP 4 users should make use of the nusoap for the same.

Creating the Web Service: The Actual Coding

In this example, we intend to create a web services dedicated to a library that would allow the users to search for books and authors and also browse the books written by a particular author.

The author is going to have the attributes, namely author_id, author_name and the book will comprise of book_id, book_name and author_id.

For the sake of simplicity, we are considering that at any point, the given writer has written only one book.

Coding for the WSDL File

The code required for creating a WSDL file is mentioned below:

There are 4 primary parts included in the WSDL File, namely:

1.Types – It defines the types which are going to be included in the program, much like defining the variations in a random PASCAL program.
2.Messages – Every function mentioned in this section has two messages primarily. One is destined for the input stream and other for the output. Besides, there is a provision for exception handling, as you get to add one extra message for the same.
3.Port Type – With the help of this feature, you get to combine one or more messages for representing the function. For instance, in the “Search Books” category, we would have to unite two different messages, one belonging to the input and other to our response.
4.Binding – Lastly, the binding section takes care of defining the details of the protocol in the web services that we are creating.

Now, we will create two different classes, one to contain the data regarding the books and other dedicated to the authors. Here, please note to keep the same name of the members, which you already specified in the WSDL file.

Class Containing the Book Data

Now we shall create a Wrapper Class:

Book Class

Now the next step is to create a PHP file which would cater to all the requests from the SOAP clients and be responsible for returning the results. Also note that this is the same file which was written in SOAP action previously specified in the WSDL file.

Lastly, please not to create a map, which would link the classes holding the data and the complex types. To know more, please consider our example in which we mapped Book, which has a complex type and Book Data which is one of the PHP classes.

Hurray! We just completed developing the web services by using PHP Development Services. Now let us name it lib.php.

Congratulations on your successful completion of the same. Please share your views on our tutorial in the comments section below.

Author Bio: Celin is a passionate Web developer and an obsessive compulsive tech writer. She is working with a leading company providing Custom PHP Development Services. You may Hire PHP Programmers for your high end customized and bespoke projects.