nusoap class or SOAP extension?
Back when the web app was to be hosted on the VPS (using PHP4), I had started coding some of the scrapers and parsers to retrieve data from different affiliate networks. After moving to the new servers and setting them up with the latest stable versions of PHP and MySQL, it was time I clean up, optimize and document my code. But no, before I could start doing that and while I was still testing it to refresh my memory on the different methods available within the DtSoap class, I started getting errors!
nusoap
My code required nusoap to communicate with the networks’ WSDL. However, just like for nusoap, PHP5 includes a class named ‘soapclient‘ which caused the conflict. The solution was simple, changing the class’ name to ‘nusoapclient’ and the constructor too before finally changing my code to call for it instead of the old name.That was the lazy-guy in me reasoning. As soon as the geek kicked in, you guessed it, or maybe not: I decided to update the code and use the built-in soap extension available in PHP5.
Sample SoapClient call (using non-WSDL mode)
$soapclient = new SoapClient(null, array(
‘location’ => ‘http://site.com/soap.php’,
‘uri’ => ‘http://soapinterop.org’
));
$parts = array(
‘client’ => ‘myclientname’,
‘password’ => ‘mypassword’,
‘customfield’ => ‘anyvalue’
);
$output = $soapclient->soapCall(’method’, $parts);
printr($output);
This worked perfectly for the first couple of records, but it inevitably had to break at some point:
Fatal error: Uncaught SoapFault exceptionThat’s because, like any other soap server, certain calls return an error with faultcode being the ‘reason’ for the error. SoapFault is an extension of the Exceptions object which acts in a way like die(), stopping execution of all code below it. By definition:
When an exception is thrown, code following the statement will not be executed (…) If an exception is not caught, a PHP Fatal Error will be issued with an “Uncaught Exception …” message, unless a handler has been defined with setexception_handler().Back to the SoapClient methods’ documentation. The constructor() method takes different options, one of which:
The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.I could set that to false and exceptions will stop being thrown, avoiding the die() effect and hopefully paving the way to my own error handling. For that, I needed a way to detect if the soap call had failed. Looking at the SoapFault object documentation, I found out about issoapfault(), a function that is described as follow:
bool issoap_fault ( mixed $obj )Bingo! It basically replaces the isarray() handling I was using with nusoap’s responses. The new code looked like that:This function is useful when you like to check if the SOAP call failed, but don’t like to use exceptions. To use it you must create a SoapClient object with the exceptions option set to zero or FALSE. In this case, the SOAP method will return a special SoapFault object which encapsulates the fault details (faultcode, faultstring, faultactor and faultdetails).
If exceptions is not set then SOAP call will throw an exception on error. issoapfault() checks if the given parameter is a SoapFault object.
$soapclient = new SoapClient(null, array( ‘location’ => ‘http://canadiansponsors.directtrack.com/api/soapaffiliate.php’, ‘uri’ => ‘http://soapinterop.org’, ‘exceptions’ => false )); $parts = array( ‘client’ => ‘myclientname’, ‘password’ => ‘mypassword’, ‘customfield’ => ‘anyvalue’ ); $output = $soap_client->__soapCall(’method’, $parts); if (is_soap_fault($output)) { if ($output->faultcode == ‘value here’) { echo ‘Erroneus response categorized in: ‘ . $output->faultcode . ‘ - More explanation could come here.’; } else { echo ‘Unknown erroneus response: ‘ . $output->faultcode; } } else { print_r($result); } There. Now that was to do exactly what this does:
$this->SoapClient = new nusoapclient('http://site.com/soap.php');
$parts = array(
'client' => 'my_client_name',
'password' => 'my_password',
'custom_field' => 'any_value'
);
$output = $this->SoapClient->call('method', $parts);
if (is_array($output)) {
if ($output['faultcode'] == 'value here') {
echo 'Erroneus response categorized in: ' . $output['faultcode'] . ' - More explanation could come here.';
} else {
echo 'Unknown erroneus response: ' . $output['faultcode'];
}
} else {
print_r($result);
}
Some benchmarks
I turned on the timers and made 5 tests for each version I came up with. Each test consisted of retrieving and formatting X set number of records. Those tests can’t be considered like final results but provide enough of a comparison given the same circumstances:- SOAP calls are made through the internet, which connection speed might vary at times.
- remote server might be busier at times.
- 19.246284 / 16.742698 / 15.762876 / 15.771755 / 15.933447 seconds, when using nusoap class.
- 10.866256 / 9.541840 / 9.518331 / 9.287219 / 9.756651 seconds, when using SOAP extension.
Trackbacks
Use this link to trackback from your own site.

