2013-06-29 34 views
0

我使用nuSOAP在PHP中使用webservice。 webservice返回一個對象數組。 當使用如何在PHP上的nuSoap上創建web服務,該服務返回可以從WCF調用的數組?

$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客戶端調用時,抱怨它不能轉換的東西[]以thingArray失敗。

回答

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客戶端將愉快地消耗SOAP響應那個的NuSOAP生成。

// 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 ... }