具有重复键的PHP Soap复杂参数

具有重复键的PHP Soap复杂参数,php,soap-client,Php,Soap Client,我需要生成以下XML <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.bloomberg.com/datalicense/dlws/ps/20071001"> <SOAP-ENV:Body> <ns1:submitGetHistoryRequest> &

我需要生成以下XML

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.bloomberg.com/datalicense/dlws/ps/20071001">
    <SOAP-ENV:Body>
        <ns1:submitGetHistoryRequest>
            <ns1:headers>
                <ns1:daterange>
                    <ns1:period>
                        <ns1:start>2018-05-08</ns1:start>
                        <ns1:end>2018-05-08</ns1:end>
                    </ns1:period>
                </ns1:daterange>
            </ns1:headers>
            <ns1:fields>
                <ns1:field>PX_LAST</ns1:field>
            </ns1:fields>
            <ns1:instruments>
                <ns1:instrument>
                    <ns1:id>US0000000002</ns1:id>
                    <ns1:yellowkey>Equity</ns1:yellowkey>
                    <ns1:type>ISIN</ns1:type>
                </ns1:instrument>
                <ns1:instrument>
                    <ns1:id>US0000000001</ns1:id>
                    <ns1:yellowkey>Equity</ns1:yellowkey>
                    <ns1:type>ISIN</ns1:type>
                </ns1:instrument>
            </ns1:instruments>
        </ns1:submitGetHistoryRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
在生成的XML中保持为空:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.bloomberg.com/datalicense/dlws/ps/20071001">
    <SOAP-ENV:Body>
        <ns1:submitGetHistoryRequest>
            <ns1:headers>
                <ns1:daterange>
                    <ns1:period>
                        <ns1:start>2018-05-08</ns1:start>
                        <ns1:end>2018-05-08</ns1:end>
                    </ns1:period>
                </ns1:daterange>
            </ns1:headers>
            <ns1:fields>
                <ns1:field>PX_LAST</ns1:field>
            </ns1:fields>
            <ns1:instruments/>
        </ns1:submitGetHistoryRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

2018-05-08
2018-05-08
最后一次

如何将选项传递给SoapClient,以使用重复的
instrument
键生成XML?

我解决了这个问题。结果表明,当您将有序数组(非关联数组)作为对象的属性传递时,使用该属性的名称生成的XML节点数与数组中的元素数相同。因此,我没有构建
SoapVar
对象并将它们放入
instruments[]
数组,而是将
instruments
数组指定为
instruments
属性:

$options->instruments = new \stdClass();

//first instrument
$instrument = new \stdClass();
$instrument->id = 'US0000000002';
$instrument->type = 'ISIN';
$instrument->yellowkey = 'Equity';

$options->instruments->instrument[] = $instrument;

//second instrument
$instrument = new \stdClass();
$instrument->id = 'US0000000001';
$instrument->type = 'ISIN';
$instrument->yellowkey = 'Equity';

$options->instruments->instrument[] = $instrument;
我在
foreach
循环中构建仪器。顺便说一句,将数组转换为对象而不是构建
stdClass
也可以正常工作:

$options->instruments->instrument[] = (object)[
    'id' => 'US0000000001',
    'type' => 'ISIN',
    'yellowkey' => 'Equity'
]
$options->instruments->instrument[] = (object)[
    'id' => 'US0000000001',
    'type' => 'ISIN',
    'yellowkey' => 'Equity'
]