thehenrys.net

April 29, 2009

.NET consume a php nusoap web service server *or* PHP NuSOAP Document/Literal Web Service Server

Filed under: Programming — dh @ 11:16 am

Ripped blatently from here so that I might not ever lose this information again!

PHP NuSOAP Document/Literal Web Service Server

If you are using regular Pear SOAP, it doesn’t work in document/literal. I tried. So I turned to NuSOAP, which unfortunately has very little in the way of documented examples, especially in document/literal. I am including the server that worked for me (discovered largely through trial and error), as well as a test client. This is designed to mirror Pear SOAP as much as possible (since I was converting from one to the other). Note: document/literal in NuSOAP does not verify data types, so int, string, boolean, etc. are all converted to strings between the client and server.

Server:

require_once("nusoap/nusoap.php");

$server = new soap_server;
$server->configureWSDL('servicename', 'urn:servicename','','document');

myRegister($server,'DoSomething',
array('in' => array('Name' => 'xsd:string',
'Age' => 'xsd:int'),
'out' => array('Pass' => 'xsd:boolean')
));

//if in safe mode, raw post data not set:
if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = implode("\r\n", file('php://input'));
$server->service($HTTP_RAW_POST_DATA);

function myRegister(&$server,$methodname,$params)
{
$server->register($methodname,$params["in"],$params["out"],
'urn:servicename', // namespace
$server->wsdl->endpoint.'#'.$methodname, // soapaction
'document', // style
'literal', // use
'N/A' // documentation
);
}

function DoSomething($Name,$Age)
{
$result=false;
if ($Name=="mleiv" && $Age==35) $result=true;
return array('Pass'=>$result);
}

Client:

require_once("nusoap/nusoap.php");

$ns="urn:servicename";
$client = new soapclient('http://localhost/wherever/SOAPServer.php?wsdl','wsdl');
if ($client->getError())
{
print "<h2>Soap Constructor Error:</h2><pre>".
$client->getError()."</pre>";
}
$params=array("Name"=>"mleiv","Age"=>35);
$result = $client->call("DoSomething",array("parameters"=>$params),$ns);
if ($client->fault) //soap_fault
{
print "<h2>Soap Fault:</h2><pre>(".$client->fault->faultcode.") ".
$client->fault->faultstring."</pre>";
}
elseif ($client->getError())
{
print "<h2>Soap Error:</h2><pre>".$client->getError()."</pre>";
}
else
{
print "<h2>Result:</h2><pre>".$result["Pass"]."</pre>";
}
print '<h2>Details:</h2><hr />'.
'<h3>Request</h3><pre>' .
htmlspecialchars($client->request, ENT_QUOTES) . '</pre>'.
'<h3>Response</h3><pre>' .
htmlspecialchars($client->response, ENT_QUOTES) . '</pre>'.
'<h3>Debug</h3><pre>' .
htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';