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.

28 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.

  5. I am not sure where you are getting your info, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

  6. Long time reader, first time commenter — so,
    thought I’d drop a comment.. — and at the same time ask for a favor.

    Your wordpress site is very simplistic – hope you don’t mind me asking
    what theme you’re using? (and don’t mind if I steal it?

    :P)

    I just launched my small businesses site –also built in wordpress
    like yours– but the theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd” on Google
    (would appreciate any feedback)

    Keep up the good work– and take care of yourself during the coronavirus scare!

    ~Justin

  7. Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?
    (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like yours– but the theme
    slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd” on Google (would appreciate any feedback) – it’s still in the works.

    Keep up the good work– and hope you all take care of yourself
    during the coronavirus scare!

  8. Hello there! This blog post couldn’t be written much better!
    Looking at this post reminds me of my previous roommate!
    He always kept talking about this. I’ll forward this
    information to him. Pretty sure he’ll have a great read. I appreciate you for sharing!

  9. We are a group of volunteers and starting a new scheme in our community. Your website offered us with valuable info to work on. You have done an impressive job and our whole community will be grateful to you.

  10. I was pretty pleased to find this site. I wanted to thank you for your time just for this fantastic read!! I definitely savored every little bit of it and i also have you saved to fav to check out new things in your site.

  11. Have you ever considered writing an e-book or guest authoring on other blogs?
    I have a blog based on the same information you discuss and would love to have
    you share some stories/information. I know my readers would appreciate your work.
    If you are even remotely interested, feel free to shoot me an e mail.

    My blog; Seattle Weekly (http://www.seattleweekly.com)

  12. Remarkable issues here. I am very happy to peer your post. Thanks a lot and I am having a look ahead to contact you. Will you kindly drop me a mail?

  13. Hello! Someone in my Myspace group shared this site with us so I came to check it out. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Great blog and outstanding design.

  14. You actually make it appear really easy together with your presentation but I in finding this topic to be really one thing which I feel I would never understand. It seems too complicated and extremely extensive for me. I am taking a look forward to your next publish, I will attempt to get the dangle of it!

Leave a Reply

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