The SoapServer class provides a server for the » SOAP 1.1 and » SOAP 1.2 protocols. It can be used with or without a WSDL service description.
If you want to return a custom object array from a nusoap webservice, you have to cast the objects to arrays like so:
<?php
$users = array();
while($res = $db_obj->fetch_row())
{
$user = new user();
$user->Id = $res['id'];
$user->Username = $res['username'];
$user->Email = $res['email'];
$users[] = (array) $user;
}
return ($users);
?>
As the SoapServer class lacks the possibility of dealing with SOAP-Headers, my following workaround is suitable to my needs when I had to do authentication via the SOAP-Headers:
1. Use a class which handles SOAP requests and let the constructor of this class take the sent headers.
2. Before invoking the SoapServer extract the SOAP-Header from the incoming raw data.
3. When using setClass pass the extracted header to the handling class.
In Your Server-Skript do things like this
<?php
$hdr = file_get_contents("php://input");
if (strpos($hdr,'<s:Header>')===false) {
$hdr = null;
} else {
$hdr = explode('<s:Header>',$hdr);
$hdr = explode('</s:Header>',$hdr[1]);
$hdr = $hdr[0];
}
$srv = new SoapServer('Your_wsdl');
$srv->setClass("ServiceClass",$hdr);
$srv->handle();
?>
And the service class is like this:
<?php
class ServiceClass {
var $IsAuthenticated;
function __construct($hdr) {
$this->IsAuthenticated = false;
if ($hdr!=null) {
$this->header = simplexml_load_string($hdr);
//Your authentication stuff goes here...
}
}
function SomeFunctionOfTheService($a) {
if ($this->IsAuthenticated) {
//here Your function...
}
}
}
?>
SoapServer does not support WSDL with literal/document. I have a class:
<?php
class My_Soap {
/**
* Returns Hello World.
*
* @param string $world
* @return string
*/
public function getInterAdmins($world) {
return 'hello' . $world;
}
}
?>
To fix this I had to create proxy class:
<?php
class My_Soap_LiteralDocumentProxy {
public function __call($methodName, $args) {
$soapClass = new My_Soap();
$result = call_user_func_array(array($soapClass, $methodName), $args[0]);
return array($methodName . 'Result' => $result);
}
}
?>
Now make sure that the WSDL is created using My_Soap. And that the Server is created using My_Soap_LiteralDocumentProxy:
<?php
if (isset($_GET['wsdl'])) {
$wsdl = new Zend_Soap_AutoDiscover(); // It generates the WSDL
$wsdl->setOperationBodyStyle(array(
'use' => 'literal'
));
$wsdl->setBindingStyle(array(
'style' => 'document'
));
$wsdl->setClass('My_Soap');
$wsdl->handle();
} else {
$server = new Zend_Soap_Server('http://localhost/something/webservice.php?wsdl');
$server->setClass('My_Soap_LiteralDocumentProxy');
$server->handle();
}
?>
Do not put a SoapServer and a SoapClient in the same PHP file. This seems to cause arbitrary behavior. On the PHP interpreter in Eclipse, everything worked fine. Under MAMP, I got an undocumented error. In moving the client from the same file as the server, everything worked fine.