SOAP Server with PHP5 – part3: the glue code

Part 2 of this series of blog posts focused on creating your wsdl. Now we will move on to the “glue code” and putting it all together. In part 2, I created a wsdl that defined the application programming interface (API) for an inventory web service. We defined one simple function called getItemCount which takes a upc in the form of a string as a parameter and returns the count in the form of an integer.

Let’s go ahead and code up the function:

<?php //inventory_functions.php
function getItemCount($upc){
//in reality, this data would be coming from a database
$items = array('12345'=>5,'19283'=>100,'23489'=>234);
return $items[$upc];
}
?>

It is important to test all of your functions outside the scope of your web service to iron out all of the bugs, otherwise, you’ll be dealing with some nasty debugging as soon as you connect this with the SOAP glue code. I like to keep the functions or classes that are connected to the web service separated from the glue code, unlike the php soap extension tutorial does. That way I can test it more easily.

The glue code

The glue code is responsible for handling incoming soap requests and returning valid soap xml. It takes the wsdl service definition and it connects it with your function or class. Here’s what it looks like:

<?php //server.php
require 'inventory_functions.php';
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("inventory.wsdl");
$server->addFunction("getItemCount");
$server->handle();
?>

The inventory_functions.php contains my getItemCount function. It is important that the function has been included (or required) before the $server->addFunction("functionName"); is called. Also, make sure that you have no white space before or after your opening and closing php tags.

Once you know that your wsdl is correct, and you won’t be changing it, remove the ini_set function from the glue code.

Important: Make sure your wsdl’s service block is pointing to the http accessible url of the glue code (see part 2 for details).

Testing the service

Now is the moment of truth, to see if your service works like you had anticipated. Simply create a script like this one:

<?php //client-test.php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$client = new SoapClient("http://[path to the service]/inventory.wsdl");
$return = $client->getItemCount('12345');
print_r($return);
?>

Substitute the [path to the service] with your url path, and execute the script. Hopefully, you should be seing the results that you expected.

Tutorial Files

For all the files that have been discussed in this tutorial, download the tutorial.zip

Sorry, this post has received a barrage of spam. I had to shut down comments. Try one of the other posts in this series.

19 thoughts on “SOAP Server with PHP5 – part3: the glue code”

  1. This is a great resource! Most books and tutorials online fail to provide enough information to make a WSDL work… and worse, are full of small bugs that are nearly impossible to find rendering the examples useless.

    You have really done a great thing here. 😀

  2. first of all thanks its really nice Tutorial. i try to implement but its not working i put files in c:\php5\pear
    also i got this error

    WSDLSOAP-ERROR: Parsing WSDL: Couldn’t load from ‘datamodel.wsdl’ : failed to load external entity “datamodel.wsdl”

    can u help me i am very greatfull to you

    thanks

  3. I tried running your code in the tutorial zip folder and i am having the following error code. Is there something i am not doing, I changed the following:

    In the server file
    $server = new SoapServer(“c:/wamp/www/tutorial/inventory.wsdl”);

    In the client file:
    $client = new SoapClient(“http://localhost/tutorial/inventory.wsdl”);

    Any help pls.

  4. sorry this the error message:
    Warning: SoapClient::SoapClient(http://localhost/tutorial/inventory.wsdl) [soapclient.soapclient]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\tutorial\client-test.php on line 3

    Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity “http://localhost/tutorial/inventory.wsdl” in C:\wamp\www\tutorial\client-test.php on line 3

    Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\tutorial\client-test.php on line 6

  5. HI I have put together my server and client and wsdl files. As soon as i add more functions to my function file my client tthrows this error.

    SoapFault exception: [Client] looks like we got no XML document in /usr/www/users/veriix/includes/Soap_client.php:5 Stack trace: #0 [internal function]: SoapClient->__call(‘age’, Array) #1 /usr/www/users/veriix/includes/Soap_client.php(5): SoapClient->age(‘veri-b503xb-sa’, ’28’, ‘lessthan’) #2 {main}

    not sure why as soon as i blank out the new function i added all works well.

    any ideas?

    thanks

  6. Just wanted to say thanks… Might be an old post, but I was able to create a working service in < 20min with no SOAP experience.

    Now, on to see what else it can do!

    Cheers

  7. Uff, it helped me a lot. I only modified:

    to:

    to accept arrays as arguments and:

    to:

    to have result in array.
    Thanks a lot!

  8. Good job. Worked like a charm. This tutorial fills a major void of currently available online SOAP/PHP/Server knowledge.

  9. hi,thanks for your article.
    when im running this code server folowing error accured:
    (not respond)
    Warning: SoapClient::SoapClient(http://localhost/tutorial/inventory.wsdl) [soapclient.soapclient]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\tutorial\client-test.php on line 3

    any help?

  10. @omid: I would have to take a look at what you have in your tutorial directory. Can you post the code to github.com or something like it so I could take a look? You can also email me directly via the contact info on the “contact me” page.

Comments are closed.