phpxmlweb-servicessoapnusoap

Returning multiple values via SOAP in PHP


tl;dr: sending multiple strings as a response to SOAP request.

I am new to SOAP. I have written a simple web service which serves request via SOAP. As I wanted to implement this in PHP, I have used NuSOAP library. The specification given to me for the SOAP API design is as follows.

REQUEST FORMAT:

<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.sandeepraju.in/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
<q0:getData> 
<token>String</token> 
</q0:getData> 
</soapenv:Body> 
</soapenv:Envelope>

EXAMPLE / SAMPLE RESPONSE:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getDataResponse xmlns:ns2="http://www.sandeepraju.in/">
<return>
<Data>
   <animal>cat</animal>
   <phone>225</phone>
   <code>FYI</code>
</Data>

The PHP code I have written for the above specification is as follows.

require_once("../nusoap_old/lib/nusoap.php");

// Definition of getData operation
function getData($token) {
    if($token == "somestring") {
        return array("animal" => "cat", "phone" => "225", "code" => "FYI");
    }
    else {
        return array("animal" => "null", "phone" => "null", "code" => "null");
    }  
}

// Creating SOAP server Object
$server = new soap_server();

// Setup WSDL
$server->configureWSDL('catnews', 'urn:catinfo');

$server->wsdl->addComplexType('return_array_php',
    'complexType',
    'struct',
    'all',
    '',
    array(
    'animal' => array('animal' => 'animal', 'type' => 'xsd:string'),
    'phone' => array('phone' => 'phone', 'type' => 'xsd:string'),
    'code' => array('code' => 'code', 'type' => 'xsd:string')
   )
);

// Register the getData operation
$server->register("getData",
    array('token' => 'xsd:string'),
    array('return' => 'tns:return_array_php'),
    'urn:catinfo',
    'urn:catinfo#getData');

// Checking POST request headers
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);

Here, I think I should NOT return a PHP array. But I am not sure what I should return according the specification. Can anyone help me with this. Or returning an array is correct?


Solution

  • You need to add another complex type for an array consisting of your data. Like this:

    $server->wsdl->addComplexType(
        'dataArray',    // MySoapObjectArray
        'complexType', 'array', '', 'SOAP-ENC:Array',
        array(),
        array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:return_array_php[]')), 'tns:return_array_php'
    );
    

    Register the new datatype to be the return-value of the function.

    $server->register(
        'getData',     
        array('Datum'=>'xsd:token'), 
        array('return'=>'tns:dataArray'),
        $namespace,
        false,
        'rpc',
        'encoded',
        'description'
    );
    

    Then your function needs to set the single parts of the array.

    function GetData($token)
    {
        if($token == "somestring") {
            $result[0] = array(
                "animal"  => "cat",
                "phone"   => "225",
                "code"    => "FYI"
            );
    
            $result[1] = array(
                "animal"  => "dog",
                "phone"   => "552",
                "code"    => "IFY"
            );
        } else {
            $result = null;
        }
        return $result;
    }
    

    The response of this service called with the string "somestring" will be:

    <ns1:getDataResponse xmlns:ns1="http://localhost/status/status.php">
        <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:return_array_php[2]">
            <item xsi:type="tns:return_array_php">
               <animal xsi:type="xsd:string">cat</animal>
               <phone xsi:type="xsd:string">225</phone>
               <code xsi:type="xsd:string">FYI</code>
            </item>
            <item xsi:type="tns:return_array_php">
               <animal xsi:type="xsd:string">dog</animal>
               <phone xsi:type="xsd:string">552</phone>
               <code xsi:type="xsd:string">IFY</code>
            </item>
        </return>
    </ns1:getDataResponse>
    

    That matches your specifications.