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

2 Responses to “.NET consume a php nusoap web service server *or* PHP NuSOAP Document/Literal Web Service Server”

  1. Rico Bonnoitt Says:

    Thanks for sharing, it is a very informative put up and very helpful for some form of companies like mine. I like when Quite possibly shopping the world wide web and i arrive throughout a website with helpful things such as this. Thanks a great deal for the investigation, I’ve noted a few of them below so I can use them in a long term. Kudos for you and sustain the great blogging do the job.

  2. Nathan Says:

    hey,

    I was wonder if you were able to get complex types to return to .net.

    For example I have a complex type:

    $server->wsdl->addComplexType(
    ‘Result’,
    ‘complexType’,
    ‘array’,
    ‘all’,
    ”,
    array(
    ‘status’ => array(‘name’ => ‘status’, ‘type’ => ‘xsd:string’),
    ‘message’ => array(‘name’ => ‘message’, ‘type’ => ‘xsd:string’),
    ‘id’ => array(‘name’ => ‘id’, ‘type’ => ‘xsd:int’)
    )
    );

    using a method:

    $server->register(
    ‘Add’, // method name
    array(‘data’ => ‘tns:Stuff’), // input parameters
    array(‘return’ => ‘tns:Result’), // output parameters
    false, // namespace
    $server->wsdl->endpoint.’#’.’Add’, // soapaction
    ‘document’, // style
    ‘literal’, // use
    ‘N/A’ // documentation
    );

Leave a Reply