Consuming (.NET) Web services using PHP and NuSoap

Posted by Andrea on 2007-10-23 21:04

In my scenario a .NET Web service offers particular encrypting capabilities.
I need to consume this Web service sending my clear string, I aspect to receive an encrypted string.
The Web service is obviously working properly.

I found this interesting library, so I adopted it:

require_once('lib/nusoap.php');

Here is the code:

// requires web service's specifications aka wsdl
// $parameters:    
// requires following the array 'notation'
// array('parameters' => (array('text' => 'Hello World!!!')));
// that equals to Hello World!!!
// $proxy (->host, ->port, ->username, -> password)
// optional, uses a proxy server

function consume_webservice($wsdl, $method, $parameters, $debug = false, $proxy = null)
{
	if ($proxy != null)
		$proxy->host = $proxy->port = $proxy->username = $proxy->password = false;

	$client = new soapclient($wsdl, true, $proxy->host, $proxy->port, $proxy->username, $proxy->password);
	$err = $client->getError();

	if ($err)
		echo '

Constructor error

' . $err . '
'; $result = $client->call($method, $parameters); // check for a fault if ($client->fault) { echo '

Fault

';
		print_r($result);
		echo '
'; } else { // check for errors $err = $client->getError(); if ($err) echo '

Error

' . $err . '
'; } if ($debug) { echo '

Request

' . htmlspecialchars($client->request, ENT_QUOTES) . '
'; echo '

Response

' . htmlspecialchars($client->response, ENT_QUOTES) . '
'; echo '

Debug

' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '
'; } return $result; }

This may work (only) with all soap Web services.
Please note that .NET Web services includes all response content into a "parameters" section.