SoapClient:如何传递多个同名元素?

发布于 2024-09-16 16:23:32 字数 1153 浏览 3 评论 0原文

我有以下代码:

$telnums = array(10, 20, 30);
$obj = new StdClass();
$obj->telnums = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums = $telnum;
}

call_user_func(array($this->client, 'createDomain'), new SoapVar($obj, SOAP_ENC_OBJECT));

$this->client 是 SoapClient 类的一个实例。

它会生成以下请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="...">
    <SOAP-ENV:Body>
        <ns1:createDomain>
            <createDomainRequest>
                <telnums>30</telnums>
            </createDomainRequest>
        </ns1:createDomain>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但我需要

            <createDomainRequest>
                <telnums>10</telnums>
                <telnums>20</telnums>
                <telnums>30</telnums>
            </createDomainRequest>

如何实现这一目标?

PS:PHP 5.2.6-3ubuntu4.5 与 Suhosin-Patch 0.9.6.2 (cli)(构建时间:2010 年 1 月 6 日 22:25:33)

提前致谢!

I have following code:

$telnums = array(10, 20, 30);
$obj = new StdClass();
$obj->telnums = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums = $telnum;
}

call_user_func(array($this->client, 'createDomain'), new SoapVar($obj, SOAP_ENC_OBJECT));

There $this->client is an instance of SoapClient class.

And it generates following request:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="...">
    <SOAP-ENV:Body>
        <ns1:createDomain>
            <createDomainRequest>
                <telnums>30</telnums>
            </createDomainRequest>
        </ns1:createDomain>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

But I need

            <createDomainRequest>
                <telnums>10</telnums>
                <telnums>20</telnums>
                <telnums>30</telnums>
            </createDomainRequest>

How I can achieve this?

P.S.: PHP 5.2.6-3ubuntu4.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan 6 2010 22:25:33)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼泪都笑了 2024-09-23 16:23:32

我最近遇到了类似的情况,我发现这种模式通常可以解决问题。

$obj = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums[] = $telnum;
}

其工作原理是因为它紧密模拟 WSDL 规定的相同数据结构

I had landed into a similar scenario recently and I found this pattern usually does the trick.

$obj = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums[] = $telnum;
}

The reason this works is because it closely emulates the same data structure as prescribed by your WSDL

小女人ら 2024-09-23 16:23:32

正确答案应该是:

$options = array(
  'createDomainRequest' => array(
    'telnums' => array(
      '10',
      '20',
      '30'
    )
  )
);

:)

The correct answer should have been:

$options = array(
  'createDomainRequest' => array(
    'telnums' => array(
      '10',
      '20',
      '30'
    )
  )
);

:)

长梦不多时 2024-09-23 16:23:32

找到可行的解决方案是一件痛苦的事,但最终并不那么难。使用 SoapParam 甚至令人惊讶地简单和整洁:

$soapClient = new SoapClient($wsdl);
$soapClient->__call('createDomain', array(
    new SoapParam('10', 'telnums'),
    new SoapParam('20', 'telnums'),
    new SoapParam('30', 'telnums'),
));

It is a pain in the buttock to find a working solution, but eventually it's not that hard. Even surprisingly easy and neat by using SoapParam's:

$soapClient = new SoapClient($wsdl);
$soapClient->__call('createDomain', array(
    new SoapParam('10', 'telnums'),
    new SoapParam('20', 'telnums'),
    new SoapParam('30', 'telnums'),
));
薔薇婲 2024-09-23 16:23:32

这是我使用的代码格式:

$wsdl = 'https://your.api/path?wsdl';
$client = new SoapClient($wsdl);
$multipleSearchValues = [1, 2, 3, 4];
$queryData = ['yourFieldName' => $multipleSearchValues];
$results = $client->YourApiMethod($queryData);
print_r($results);

Here is the code format that I used:

$wsdl = 'https://your.api/path?wsdl';
$client = new SoapClient($wsdl);
$multipleSearchValues = [1, 2, 3, 4];
$queryData = ['yourFieldName' => $multipleSearchValues];
$results = $client->YourApiMethod($queryData);
print_r($results);
望喜 2024-09-23 16:23:32

通过扩展 SoapClient 并覆盖 __doRequest() 方法来修复它,我按照此处所述修改请求: http://www.php.net/manual/en/soapclient.dorequest.php#57995

对我来说看起来很糟糕,但它“就在这里,现在”工作。

Fixed it by extends SoapClient and overrides __doRequest() method where I modify request as descibed here: http://www.php.net/manual/en/soapclient.dorequest.php#57995

Looks terrible for me, but it works "right here, right now".

巷雨优美回忆 2024-09-23 16:23:32
$telnums=array(10, 20, 30);
$createDomainRequest=array('createDomainRequest' => array(
  'telnums' => $telnums)
);
$telnums=array(10, 20, 30);
$createDomainRequest=array('createDomainRequest' => array(
  'telnums' => $telnums)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文