You are here: Home » Programming » Web Tricks » PHP NuSOAP Document/Literal Web Service Server

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>';	

4 Comments

Thank you for posting this. I've been looking everywhere for an example of document literal style with nusoap, as I've been having return problems. Bravo.

I feel your pain. Or rather felt. ;)

It doesn't seem to work on PHP 5.2 and NUSOAP 0.7.1. This is the return error message

XML error parsing SOAP payload on line 2: Invalid document end

Well, I haven't used this in awhile (and the project has since been abandoned), but I recommend going to the wsdl url directly from line 2 in client and seeing if you can spot a better error message (the error is in what the server is sending, pretty sure). You may need to add some try/catches or something to make it be more helpful.

Leave a comment


Type the characters you see in the picture above.