Search code examples
phpnusoap

Return two types with NuSOAP


I have a working webService using NuSOAP. Now, I have to make a validation before returning the data requested. If everything is ok, I return it normally, otherwise I would like to return a String message explaining why I'm not giving the information requested. Problem is that I can't get to add two different types of return to RegisterFunction of NuSOAP. If I add a ComplexType as return, I can't return a String.


Solution

  • The function can't have two return-values. You should add the error-message-string to your complex type. If you don't wanna touch your complex type, then you should create another complex type wich contains your datatype and a string.

    Example - the complex type you have right now:

    $server->wsdl->addComplexType('myData','complexType','struct','all','',
        array(  'important'  => array('name' => 'important','type' => 'xsd:string'),
                'stuff'      => array('name' => 'stuff','type' => 'xsd:string')
        )
    );
    

    the extra complex type:

    $server->wsdl->addComplexType('package','complexType','struct','all','',
        array(  'data'      => array('name' => 'data','type' => 'tns:myData'),
                'errormsg'  => array('name' => 'errormsg','type' => 'xsd:string')
        )
    );
    

    registration of the function:

    $server->register(
                    'getData',     
                    array('validation'=>'xsd:string'), 
                    array('return'=>'tns:package'),
                    $namespace,
                    false,
                    'rpc',
                    'encoded',
                    'description'
    );
    

    the function:

    function GetData($validation)
    {
        if($validation == "thegoodguy") {
            $result['data'] = array(
                "important"    => "a top secret information",
                "stuff"        => "another one"
            );
            $result['errormsg'] = null;
        } else {
            $result['data'] = null;
            $result['errormsg'] = "permission denied!";
        }
        return $result;
    }
    

    That way the client could try to analyse the received data and if it is null then he shows up the errormessage.