PHP Web Services: What they are, what they do and how to make one.

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

  1. 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.
  2. Create a new project folder on server.
  3. Install the PHP NUSOAP library: Download it from here.
  4. Extract the archive and keep the lib folder in your project folder.
  5. 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 ?>
  6. Create a SOAP server object.
    <?php $soapServer = new soap_server(); //create the soap server object ?>
  7. Configure WSDL
    <?php $soapServer-&gt;configureWSDL("HelloWorldWSDL", "http://www.helloworldsite.com"); //configure the WSDL ?>
  8. Register a method with your SOAP server.
    <?php $soapServer-&gt;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 ?>
  9. Make sure you call the service method of the SOAP server.
    <?php /*$soapServer-&gt;service($HTTP_RAW_POST_DATA); //Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn't parse! */ @$soapServer-&gt;service(file_get_contents("php://input")); //FIXED ?>
  10. 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'=&amp;gt;'xsd:string'),array('return'=&amp;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

  1. Check SOAP is enabled on the server: This step is identical to its counterpart in the server-side above.
  2. Create a new project folder on server: This step is identical to its counterpart in the server-side above.
  3. Install the PHP NUSOAP library: This step is identical to its counterpart in the server-side above.
  4. 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.
  5. Create a SOAP client object.
    <?php $client = new nusoap_client("http://localhost/HelloSvc/hello_world_web_svc.php?wsdl"); ?>
  6. Call the server-side method.
    <?php $response = $client-&gt;call("helloWorldWebSvc", array("Some User")); //calling the registered method of the web service from client. ?>
  7. 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.

14 thoughts on “PHP Web Services: What they are, what they do and how to make one.”

  1. That is a really good tip especially to those fresh to the blogosphere.
    Brief but very accurate info… Appreciate your sharing
    this one. A must read article!

  2. Right here is the perfect website for everyone who would like to understand this topic.
    You know so much its almost tough to argue with you (not that I actually would
    want to…HaHa). You definitely put a new spin on a subject that has been discussed for
    decades. Wonderful stuff, just excellent!

  3. I am extremely inspired together with your writing skills as
    well as with the structure on your blog. Is that this a paid theme or did you customize
    it your self? Anyway stay up the excellent high quality writing, it is uncommon to look a
    nice weblog like this one today.

  4. Nice post. I was checking constantly this blog and I am impressed! Very useful info specifically the last part 🙂 I care for such info a lot. I was looking for this particular info for a long time. Thank you and good luck.

Leave a Reply

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