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


![[bluehost]](http://mleiv.com/wp-content/files/site_images/bluehost.gif)
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
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.
You can download the nusoap.php file from the sourceforge archive.
The latest release provides you with a modern example of a WSDL document literal Client
http://sourceforge.net/projects/nusoap/
Thanks for your sample, I got it running with nusoap 0.7.3. Only a small modification had to be done: in the SOAPClient create a nusoap_client instead of a soapclient. However, additionally I had to strip all occurences of semicola inside
tags. Why such a strange notation?
PS.: if I want to make the Request not from PHP, I have to declare the usage of utf-8 encoding. (I dunno why, perhaps the reason are my browsersettings, as I send the Request with the help of Javascript)
There was a conflict between Pear SOAP and nuSOAP originally - I'm guessing they've renamed the client object to avoid that?
And the semi-colons.... haha. I'm guessing those were an import/export MovableType something bug. They shouldn't be there. Off to remove them from the sample *right now*. :)
I wrote a simple helloworld server and client codes all wsdl and other functions are working. But I want to identify the requesting client in server code and respond (To set in return value in hellowold function) that client address back to client. Please help me to solve this.
Cleint code:
require_once('./lib/nusoap.php');
$client = new nusoap_client('http://localhost/services/server.php?wsdl', 'true');
echo $result = $client->call('helloworld');
Server code: (server.php)
require_once('lib/nusoap.php');
$server = new soap_server();
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
$server->register('helloworld',
array('name' => 'xsd:string'),
array('return' => 'xsd:string'),
'urn:hellowsdl',
'urn:hellowsdl#hello',
'rpc',
'encoded',
'Says hello to the caller'
);
function helloworld()
{
return "Hi, ";
// I want to return requester(Client) address (Ip or url) here..................
// Your address is .............
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
Thanks,
Samudaya.
These php variables may help you: http://us.php.net/manual/en/reserved.variables.server.php
For instance,
> return $_SERVER['REMOTE_ADDR'];
would return the IP address.
Brilliant article! Well done! ;o)
Thanks mleiv
Thanks for this article. It helped me get out of an irritating break in my development task.
Hi,
i am using nusoap 0.7.3 and php 2.5.6. I am creating one server on my live site and fetch data using client. So my problem is when i fetch data from the server using nusoap i got response in the format of the string not the text/xml. But i return xml from my server. At client side i have php 2.5.6. So i run my code at lower version of php so it retrun perfect xml .
i see the xml in the viewsource it not contain any html tag instaed of html tag it display string like ' '<' this way so my data is not consider as a valid xml.
Please guide me what i do for perfect xml.
My client code is running perfect on some pc and some pc it is not work.
Getting the following error message :
Response not of type text/xml: text/html
Any one got an idea?
Thanks
That usually indicates a php error in the response -> it is returning the text of the php error instead of an xml response.
Dear,
I am using nusoup for daisy online delivery protocol. Her i used "Document" "literal" as style and use. I have to return complex array type.It is returning empty record.
But it working fine in "rpc" style.Please advice me how return commplex array type in document-literal.
Thanks in advance
Manivasagan S
Hi, Thanks for this info.
Can you please tell me how to implement this for multidimensional array as response.
I don't remember all the details, but you'd likely have to define a new data type the to do that (which looked scary). The easier (cheat) method would be to send the array as a serialized string, provided you are using php on both sides and do a fair bit of data verification to confirm it is correct.