NuSOAP is a rewrite of SOAPx4, provided by NuSphere and Dietrich Ayala. It is a set of PHP classes – no PHP extensions required – that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1. Clik here to can download NuSOAP Toolkit for PHP.
Steps to use NuSOAP Toolkit:
Step1: Download NuSOAP Toolkit
Step2: Extract the downloaded NuSOAP Toolkit folder into your project location under library folder.
Step3: Include the NuSOAP PHP library file into your application file like this “require_once ‘lib/nusoap.php'”. Now you are ready to use NuSOAP library file into your project.
Method 1:
[php]
// include nusoap class file
require_once(‘lib/nusoap.php’);
// invoke webservice url
$client = new nusoap_client("http://test.wsdl.com/soap2?wsdl", $wsdl = true);
// detect any error occured while invoke webservice
$err = $client->getError();
if ($err) {
echo ‘<h2>Constructor error</h2><pre>’ . $err . ‘</pre>’;
echo ‘<h2>Debug</h2><pre>’ . htmlspecialchars($client->getDebug(), ENT_QUOTES) . ‘</pre>’;
exit();
}
// parameters for webservice method
$params = array(
‘manufacturer’ => "O’Reilly",
‘mode’ => ‘books’,
‘name’ => ‘lets c’,
);
// invoke webservice method with respective parameters
$result = $client->call(‘SearchRequest’, $params);
// detect if any error occured
if ($client->fault) {
echo ‘<h2>Fault (Expect – The request contains an invalid SOAP body)</h2><pre>’;
print_r($result); echo ‘</pre>’;
} else {
$err = $client->getError();
if ($err) {
echo ‘<h2>Error</h2><pre>’ . $err . ‘</pre>’;
} else {
echo ‘<h2>Result</h2><pre>’;
print_r($result); echo ‘</pre>’;
}
}
// to view SOAP webservice request XML
echo ‘<h2>Request</h2><pre>’ . htmlspecialchars($client->request, ENT_QUOTES) . ‘</pre>’;
// to view SOAP webservice response XML
echo ‘<h2>Response</h2><pre>’ . htmlspecialchars($client->response, ENT_QUOTES) . ‘</pre>’;
// to view SOAP webservice response result in step by step, it helps to debug
echo ‘<h2>Debug</h2><pre>’ . htmlspecialchars($client->getDebug(), ENT_QUOTES) . ‘</pre>’;
[/php]
Method 2:
[php]
// include nusoap class file
require_once(‘lib/nusoap.php’);
// invoke webservice url
$client = new nusoap_client("http://test.wsdl.com/soap2?wsdl", $wsdl = true);
$client->soap_defencoding = ‘utf-8’;
$client->useHTTPPersistentConnection(); // Uses http 1.1 instead of 1.0
// detect any error occured while invoke webservice
$err = $client->getError();
if ($err) {
echo ‘<h2>Constructor error</h2><pre>’ . $err . ‘</pre>’;
echo ‘<h2>Debug</h2><pre>’ . htmlspecialchars($client->getDebug(), ENT_QUOTES) . ‘</pre>’;
exit();
}
$request_xml = ‘
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<username>username1</username>
<password>test1234</password>
</soapenv:Header>
<soapenv:Body>
<m:processRequest>
<sOAPElement>
<searchRequest>
<query>books</query>
</searchRequest>
</sOAPElement>
</m:processRequest>
</soapenv:Body>
</soapenv:Envelope>
‘;
// invoke webservice with data
$result = $client->send($request_xml);
// detect if any error occured
if ($client->fault) {
echo ‘<h2>Fault (Expect – The request contains an invalid SOAP body)</h2><pre>’;
print_r($result); echo ‘</pre>’;
} else {
$err = $client->getError();
if ($err) {
echo ‘<h2>Error</h2><pre>’ . $err . ‘</pre>’;
} else {
echo ‘<h2>Result</h2><pre>’;
print_r($result); echo ‘</pre>’;
}
}
// to view SOAP webservice request XML
echo ‘<h2>Request</h2><pre>’ . htmlspecialchars($client->request, ENT_QUOTES) . ‘</pre>’;
// to view SOAP webservice response XML
echo ‘<h2>Response</h2><pre>’ . htmlspecialchars($client->response, ENT_QUOTES) . ‘</pre>’;
// to view SOAP webservice response result in step by step, it helps to debug
echo ‘<h2>Debug</h2><pre>’ . htmlspecialchars($client->getDebug(), ENT_QUOTES) . ‘</pre>’;
[/php]
To extract the response result from the SOAP webservice, follow the below steps:
[php]
// invoke webservice with data
$result = $client->send($request_xml);
// invoke webservice method with respective parameters
$result = $client->call(‘SearchRequest’, $params);
// by using both method you will get the results in $results variable, to view the results of webservice
echo ‘<h2>Result</h2><pre>’;
print_r($result); echo ‘</pre>’;
// To get attribute value of XML node
echo $result[‘searchResponse’][‘books’][‘!ID’];
// To get XML node value
echo $result[‘searchResponse’][‘books’][‘author’];
[/php]
If you have any queries on the above code, Please drop your comments.