Build a Web service using PHP

Web services provide a standard means of communication between different software applications, running on a variety of platforms and/or frameworks. Web services plays important role in any SOA architecture. I worked with couple of web services project that were developed using Java/J2EE. I was wondering how the same services can be implemented in PHP. Though I didn’t feel the web services support in PHP is as strong as Java and other technologies, but we can work with it. I tried web services functionality that was built in with PHP but later zeroed in on the third party library NuSOAP. NuSOAP provides single php file that you have to include in your code and your ready to implement web services. Here I have tried simple web service example in PHP, with a soap server and soap client.

You can see I have included single third party nusoap.php to achieve this functionality of SOAP Server. NuSOAP provides wsdl support easily. To see the wsdl you need to type in this url in the browser http://localhost/soap-server.php?wsdl. You will be able to see wsdl xml like belwo

The web service is supporting two simple methods add and multiply that are exposed to outer world.

Now the next task is to consume these web services. For that I have simple file, thats going to create soap client using NuSOAP and call the service. The code sample is attached here. Its simple and staraight forward. I have parameterized it for ease of testing. Test url will look like http://localhost/soap-client.php?method=multiply&param1=56&param2=67

If you go through this code you will see how easy it to call a web service in PHP.

Here is the web service test through SaopUI, a third party web service testing tool.

PHP Web service Testing - SoapUI

PHP Web service Testing – SoapUI

That’s it for this article. I will try to give some example in next on how to consume this web service in Java.

Add a Comment

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