1

tl;dr:发送多个字符串作为对 SOAP 请求的响应。

我是 SOAP 新手。我编写了一个简单的 Web 服务,它通过 SOAP 提供请求。因为我想在 PHP 中实现它,所以我使用了NuSOAP库。给我的 SOAP API 设计规范如下。

请求格式:

<?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>

示例/示例响应:

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

我为上述规范编写的 PHP 代码如下。

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

在这里,我认为我不应该返回PHP 数组。但我不确定我应该根据规范返回什么。谁能帮我这个。或者返回一个数组是正确的?

4

1 回答 1

3

您需要为包含数据的数组添加另一种复杂类型。像这样:

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

将新数据类型注册为函数的返回值。

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

然后你的函数需要设置数组的单个部分。

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

使用字符串“somestring”调用的此服务的响应将是:

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

这符合您的规格。

于 2013-01-07T09:53:09.507 回答