SOAP Server with PHP5 – part3: the glue code 9

Posted by Jimmy'z on February 20, 2007

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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. David Freeman Thu, 15 Mar 2007 13:35:20 UTC

    Thanks for this great resource. This really helped me out a lot.
    DCF

  2. oo oo Sun, 25 Mar 2007 13:45:45 UTC

    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. :-D

  3. ali Tue, 03 Jun 2008 08:22:06 UTC

    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

  4. Maarten De Block Fri, 19 Dec 2008 06:09:16 UTC

    Thank you very much!

  5. Kiptieu Mon, 02 Mar 2009 14:11:58 UTC

    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.

  6. Kiptieu Mon, 02 Mar 2009 14:13:23 UTC

    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

  7. Philip Fri, 13 Mar 2009 01:09:06 UTC

    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

  8. Philip Fri, 13 Mar 2009 01:20:12 UTC

    Hey No Worries my code was abit wrong was missing a couple of ;; and $$ you know..

    thanks for the great tutorial.

    Cheers

  9. ElMehdi Sat, 16 May 2009 16:13:58 UTC

    Hello,
    Thank you for your tutorial : it’s clean and well detailed.
    Regards,
    ElMehdi

Comments