2011-06-08 48 views
0

我想通過肥皂呼叫發送多個重複的元素。我嘗試構建一個數組併發送,但它只處理第一個元素。下面的帖子很有幫助,因爲我使用SoapVar SoapVar/Param and nested, repeated elements in SOAP修改了代碼。然而,我現在面臨的問題是,當我嘗試發送我的soap請求時,soap調用需要將該請求作爲一個數組,並且以下代碼從soap服務器中獲取失敗。我正在使用的WSDL文件位於https://ecomapi.networksolutions.com/soapservice.asmx?wsdl肥皂在PHP中調用多個元素 - 需要傳入數組?

我已經替換了頭部元素的安全性(我的名字,我的證書編號,我的令牌) - 但除此之外,完整的代碼如下。有什麼想法,我在做什麼錯在這裏?

<?php 

$ns = 'urn:networksolutions:apis'; 
$header->Application = 'my name'; 
$header->Certificate = 'my cert number'; 
$header->UserToken = 'my token'; 

$credentials = new SOAPHeader($ns, "SecurityCredential", $header); 

$client = new SoapClient('https://ecomapi.networksolutions.com/soapservice.asmx?wsdl', 
       array('soap_version' => SOAP_1_1 , 
           'trace' => 1)); 
$array1=array(); 
$array1[]=new SoapVar("9",XSD_STRING,null,null,'ProductId'); 
$array1[]=new SoapVar("500",XSD_STRING,null,null,'QtyInStock'); 
$soap1 = new SoapVar($array1, SOAP_ENC_OBJECT, null, null, "Inventory"); 
$interim = array($soap1); 
$test = array(); 
$test[] = new SoapVar($interim, SOAP_ENC_OBJECT, null, null, "UpdateInventoryRequestList"); 


$array2=array(); 
$array2[]=new SoapVar("10",XSD_STRING,null,null,'ProductId'); 
$array2[]=new SoapVar("500",XSD_STRING,null,null,'QtyInStock'); 
$soap2 = new SoapVar($array2, SOAP_ENC_OBJECT, null, null, "Inventory"); 
$interim2 = array($soap2); 
$test[] = new SoapVar($interim2, SOAP_ENC_OBJECT, null, null, "UpdateInventoryRequestList"); 

$submit1 = array($test); 
$submit = new SoapVar($submit1, SOAP_ENC_OBJECT, null, null, "PerformMultipleRequest"); 
$final_submit = array($submit); 

$result = $client->__soapCall("PerformMultiple", $final_submit, NULL, $credentials); 

echo "REQUEST:\n" . $client->__getLastRequest() . "\n"; // gets last SOAP request 
echo "RESPONSE:\n" . $client->__getLastResponse() . "\n"; // gets last SOAP respone 

?> 

回答

1

爲什麼這麼複雜?

PHP的SoapClient的自動完成所有的東西給你:

$array = array(); 
$array[] = new Inventory(50, 100); 
$array[] = new Inventory(51, 10); 
$client = new SoapClient('https://ecomapi.networksolutions.com/soapservice.asmx?wsdl', 
       array('soap_version' => SOAP_1_1, 'trace' => 1)); 
$client->PerformMultiple($array); 

的庫存需要是這樣的:

class Inventory 
{ 
    public $ProductId; 
    public $QtyInStock; 

    public function __construct($id, $qty) 
    { 
     $this->ProductId = $id; 
     $this->QtyInStock = $qty; 
    } 
} 

我使用許多SOAP的服務的方式。只有另一種方式有點棘手,因爲數組參數不能直接訪問,例如,如果您希望$ param爲ObjectClass [],您可以從$ param-> ObjectClass中獲取該數組。

關於

+0

啊謝謝。我以爲我不需要使用SoapVar,但我無法實現它的工作!這是我第一次使用Stack Overflow - 我應該早點來這裏 - 我非常感謝你的幫助。 – Hayden 2011-06-08 18:06:15