【问题标题】:PHP Soap complex params with repeated keys具有重复键的 PHP Soap 复杂参数
【发布时间】:2018-05-29 15:35:57
【问题描述】:

我需要生成以下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"
);

&lt;ns1:instruments/&gt; 在生成的 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>

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

【问题讨论】:

    标签: php soap-client


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多