PHP Soap复杂参数与重复键

时间:2018-05-29 15:35:57

标签: 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>
            <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>

我使用PHP 7.1.17和SoapClient。

我无法将选项传递给SoapClient,因为重复instrument键并且PHP的关联数组不能两次使用相同的键。我尝试构建对象并将instrument属性设置为SoapVar,但它会生成不正确的XML。这是代码和结果:

$options = new \stdClass();
$options->headers = new \stdClass();
$options->headers->daterange = new \stdClass();
$options->headers->daterange->period = new \stdClass();
$options->headers->daterange->period->start = '2018-05-08';
$options->headers->daterange->period->end = '2018-05-08';
$options->fields = new \stdClass();
$options->fields->field = 'PX_LAST';

//first instrument
$instrument = new \stdClass();
$instrument->id = 'US0000000002';
$instrument->type = 'ISIN';
$instrument->yellowkey = 'Equity';
$options->instruments[] = new \SoapVar(
    $instrument,
    SOAP_ENC_OBJECT,
    'stdClass',
    "http://soapinterop.org/xsd",
    "instrument"
);
//second instrument
$instrument = new \stdClass();
$instrument->id = 'US0000000001';
$instrument->type = 'ISIN';
$instrument->yellowkey = 'Equity';
$options->instruments[] = new \SoapVar(
    $instrument,
    SOAP_ENC_OBJECT,
    'stdClass',
    "http://soapinterop.org/xsd",
    "instrument"
);
结果XML中

<ns1:instruments/>仍然为空:

<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>

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

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。当您将有序数组(非关联数组)作为对象的属性传递时,结果是使用该属性的名称生成许多XML节点,作为数组中元素的数量。因此,我没有构建instruments[]个对象并将它们放入instruments数组中,而是将instrument的工具数组分配为$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' ] 也可以正常工作:

{{1}}