0

我有一个使用 nuSOAP 的 PHP 网络服务。Web 服务返回一个对象数组。使用时

$server->wsdl->addComplexType(
    "thingArray",         // type name
    "complexType",        // soap type
    'array',              // php type (struct/array)
    'sequence',           // composition (all/sequence/choice)
    '',                   // base restriction
    array(                // elements
       'item' => array(
           'name' => 'item',
           'type' => 'tns:thing',
           'minOccurs' => '0',
           'maxOccurs' => 'unbounded'
       )
   ),
   array(),              // attributes
   "tns:thing"           // array type
);

WCF 客户端在调用时失败,抱怨它无法将 thing[] 转换为 thingArray。

4

1 回答 1

0

首先 - 记得打开 UTF8 以便 WCF 可以理解响应。

  // Configure UTF8 so that WCF will be happy
  $server->soap_defencoding='UTF-8';
  $server->decode_utf8=false;

为了让 WCF 理解数组,我们需要对数组使用 SOAP 编码而不是序列组合。

这将使 nuSOAP 发出一个 WCF 可以使用的数组:

  $server->wsdl->addComplexType(
      'thingArray',          // type name
      'complexType',         // Soap type
      'array',               // PHP type (struct, array)
      '',                    // composition
      'SOAP-ENC:Array',      // base restriction
      array(),               // elements
      array(                 // attributes
        array(
          'ref'=>'SOAP-ENC:arrayType',
          'wsdl:arrayType'=>'tns:thing[]'
        )
      ),      // attribs
      "tns:thing"        // arrayType
  );

这种类型现在可以在响应中使用,WCF 客户端将愉快地使用 nuSOAP 生成的 SOAP 响应。

  // Register the method to expose
  $server->register('serviceMethod',           // method name
      array('param1' => 'tns:thingArray'),     // input parameters
      array('return' => 'tns:thingArray'),     // output parameters
      $ns,                                     // namespace
      $ns.'#serviceMethod',                    // soapaction
      'rpc',                                   // style
      'encoded',                               // use
      'Says hello'                             // documentation
  );

WCF 客户端最终看起来像这样:

   var client = new Svc.servicePortTypeClient();
   thing[] things = new thing[3];
   thing[] result = client.serviceMethod(things);
   foreach( thing x in result )
   { ... do something with x ... }
于 2013-06-29T20:59:19.803 回答