A “Web Service” is a bunch of code that resides on a server in an intranet or the internet. It performs some function and returns some data required by the client application that calls it. e.g. A foreign exchange web service might return the rates at which euros are converted to US dollars. A weather web service will return local temperature during any day. These web services can then be used in client applications like an e-commerce site that shows product prices in euros or dollars depending on the country the buyer is in, etc.
One common use of web services is in smartphone apps as a backend. A smartphone does not typically query a site’s database directly since it is not good to store database credentials locally on it. So the app calls a web service residing on a server. This web service fetches data from a web application and returns the data found to the smartphone app. e.g. An mobile e-commerce store allows users to search products by keywords. This app will call a web service which acts like a function that will accept the search keyword as a parameter. The web service returns a list of products to the calling app either as JSON or XML. The calling app will parse the results and display a list of products with names and images to the buyer.
But how is this achieved from the coding standpoint ? How does one create a PHP web service? There are many ways but I shall discuss about SOAP-based web services and illustrate with an example containing both the web service and the calling code in PHP. I will be using the NUSOAP library in my example. This will be an parameterized hello world web service. The client will call the web service and pass a name as a parameter to it. The result will be a
“Hello” greeting to the name given.
We need to do two things, (1) Setup the server side and (2) Setup the client side, from which we will call our web service.
DISCLAIMER: It is assumed that the reader knows basics of PHP development and can work on web servers or locally setup development servers.
Setting Up The Server Side
- Check SOAP is enabled on the server: Your server phpinfo() should look like this.If your phpinfo() does not look like the screenshot above, you need to locate your php.ini file and uncomment the semi-colon in the lines containing the soap DLL file (php_soap.dll in Windows). Save the modified php.ini and restart Apache Server. Then check your phpinfo() again.
- Create a new project folder on server.
- Install the PHP NUSOAP library: Download it from here.
- Extract the archive and keep the lib folder in your project folder.
- Call lib/nusoap.php in your PHP file where you will be working.
<?php require('lib/nusoap.php'); //make sure that the nusoap.php file is found at this path relative to your php source file ?>
- Create a SOAP server object.
<?php $soapServer = new soap_server(); //create the soap server object ?>
- Configure WSDL
<?php $soapServer->configureWSDL("HelloWorldWSDL", "http://www.helloworldsite.com"); //configure the WSDL ?>
- Register a method with your SOAP server.
<?php $soapServer->register('helloWorldWebSvc', array('name'=>'xsd:string'),array('return'=>"xsd:string"), "http://www.helloworldsite.com", "http://www.helloworldsite.com#helloWorldWebSvc", 'rpc', 'encoded', 'Hello World App!'); //register the method with the server ?>
- Make sure you call the service method of the SOAP server.
<?php /*$soapServer->service($HTTP_RAW_POST_DATA); //Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn't parse! */ @$soapServer->service(file_get_contents("php://input")); //FIXED ?>
Putting It All Together
<?php require("lib/nusoap.php"); $soapServer = new soap_server(); //create the soap server object $soapServer->configureWSDL("HelloWorldWSDL", "http://www.helloworldsite.com"); //configure the WSDL $soapServer->register('helloWorldWebSvc', array('name'=&gt;'xsd:string'),array('return'=&gt;"xsd:string"), "http://www.helloworldsite.com", "http://www.helloworldsite.com#helloWorldWebSvc", 'rpc', 'encoded', 'Hello World App!'); //register the method with the server function helloWorldWebSvc($name) //our function with business logic. Also called the server-side method. { return "Hello $name!"; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:''; /*$soapServer->service($HTTP_RAW_POST_DATA); //Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn't parse! */ @$soapServer->service(file_get_contents("php://input")); //FIXED exit; ?>
Important Note
It is important to note that there are outdated code snippets out there using $soapServer->service($HTTP_RAW_POST_DATA);
This wont work with NUSOAP in PHP 7. It will give the error:
Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn’t parse!
Fix it by using the following:
@$soapServer->service(file_get_contents(“php://input”));
Setting Up the Client Side
- Check SOAP is enabled on the server: This step is identical to its counterpart in the server-side above.
- Create a new project folder on server: This step is identical to its counterpart in the server-side above.
- Install the PHP NUSOAP library: This step is identical to its counterpart in the server-side above.
- Call lib/nusoap.php in your PHP file where you will be working: This step is identical to its counterpart in the server-side above.
- Create a SOAP client object.
<?php $client = new nusoap_client("http://localhost/HelloSvc/hello_world_web_svc.php?wsdl"); ?>
- Call the server-side method.
<?php $response = $client->call("helloWorldWebSvc", array("Some User")); //calling the registered method of the web service from client. ?>
Putting It All Together
<?php require("lib/nusoap.php"); $client = new nusoap_client("http://localhost/HelloSvc/hello_world_web_svc.php?wsdl"); $response = $client->call("helloWorldWebSvc", array("Some User")); //calling the registered method of the web service from client if($client->fault) { echo "Fault:".$client->faultcode." ".$client->faultstring; } else { print_r($response); } exit; ?>
Note: In Android and iOS apps, no PHP client is used generally. Instead coding is done in Java or Objective-C, etc that uses the HTTP Client API of the respective programming languages.
The End Result
If all goes well you will see a screen like the one below when you invoke the PHP Web Service client in your browser.
Note: This example is compliant with PHP 7.