使用多个元素在PHP中进行肥皂调用 - 需要传入As数组?

时间:2011-06-08 13:46:22

标签: php soap

我试图通过Soap Call发送多个重复的元素。我尝试构建一个数组并发送,但它只处理第一个元素。以下帖子很有帮助,因为我使用SoapVar SoapVar/Param and nested, repeated elements in SOAP修改了代码。但是,我现在面临的问题是,当我尝试发送我的soap请求时,soap调用需要将请求作为数组,并且以下代码从soap服务器获取失败。我正在使用的WSDL文件位于https://ecomapi.networksolutions.com/soapservice.asmx?wsdl

我已经替换了标题元素以获得安全性(我的名字,我的证书编号,我的令牌) - 但除此之外,完整的代码如下所示。对我在这里做错了什么的想法?

<?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 个答案:

答案 0 :(得分:1)

为什么这么复杂?

PHPs 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-Services。只有相反的方法有点棘手,因为作为数组的参数不能直接访问,例如,如果你希望$ param是ObjectClass []你从$ param-&gt; ObjectClass获得该数组。

问候