Developing SOAP Clients using PHP - Debugging NuSOAP (
Page 2 of 4 )
NuSOAP has some debugging features also. They are:
- $client->request: This is used to get the SOAP request message.
- $client->response: This is used to get the SOAP response message.
- $client->debug_str: This is used to get the debug information.
- $client->fault: This is used to get faults during a remote method call.
We can use all of these to check and display all errors. Here is a complete example.
<?php
require_once('nusoap.php');
$client = new soapclient('http://localhost/add.php');
$err = $client->getError();
if ($err) {
echo '<p><b>Constructor error: ' . $err . '</b></p>';
}
else{
$result = $client->call('add', array('a' => 5, 'b' => 7));
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
echo '<p><b>Request: <br>';
echo htmlspecialchars($client->request, ENT_QUOTES) . '</b></p>';
echo '<p><b>Response: <br>';
echo htmlspecialchars($client->response, ENT_QUOTES) . '</b></p>';
echo '<p><b>Debug: <br>';
echo htmlspecialchars($client->debug_str, ENT_QUOTES) . '</b></p>';
} else {
$err = $client->getError();
if ($err) {
echo '<p><b>Error: ' . $err . '</b></p>';
echo '<p><b>Request: <br>';
echo htmlspecialchars($client->request, ENT_QUOTES) . '</b></p>';
echo '<p><b>Response: <br>';
echo htmlspecialchars($client->response, ENT_QUOTES) . '</b></p>';
echo '<p><b>Debug: <br>';
echo htmlspecialchars($client->debug_str, ENT_QUOTES) . '</b></p>';
} else {
print_r($result);
}
}
}
?>
Here we are creating a soapclient object to access the add web service. If there is any error during construction, we are printing this error. If there is no error, we are calling this remote method. If this method returns any fault then we are printing the result of this function along with the request and response SOAP messages, debug string as well. If there is no fault then we are trying to get the errors that occurred during the last method call. If there are any errors then we are printing these errors along with the SOAP request, response and debug string. If there is no error then we are printing the server response. Here is a demo SOAP request message.
POST /add.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: ""
Content-Length: 560
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>
<ns1:add xmlns:ns1="http://testuri.org">
<a xsi:type="xsd:integer">5</a>
<b xsi:type="xsd:integer">7</b>
</ns1:add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Here is a demo SOAP response message.
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 25 Jul 2007 21:32:34 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
Content-Length: 546
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>
<ns1:addResponse xmlns:ns1="http://tempuri.org">
<return xsi:type="xsd:integer">12</return>
</addResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Using NuSOAP is very simple for developing PHP web clients as described in previous code examples. Now we will try to learn about PEAR::SOAP.