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.
Filed Under: Web Services
David Freeman said,
Wrote on March 15, 2007 @ 1:35 pm
Thanks for this great resource. This really helped me out a lot.
DCF
oo oo said,
Wrote on March 25, 2007 @ 1:45 pm
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.
ali said,
Wrote on June 3, 2008 @ 8:22 am
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