在PHP本地soapClient中的SoapRequest格式化。

奇怪的情况下使用 SoapClient 我有以下代码来启动一个简单的soap请求。

        $client = new \SoapClient($this->wsdlURI, [
            'trace' => 1,
            'cache_wsdl' => WSDL_CACHE_NONE,
            'soap_version' => 'SOAP_1_2',
            'features' => SOAP_SINGLE_ELEMENT_ARRAYS|SOAP_USE_XSI_ARRAY_TYPE
        ]);
        $request = [
            'dataRequest' => [
                'UserId' => '***',
                'AccountNumber' => (string) $customer->getAccountNumber(),
                'PremiseAddress' => [
                    'ZipCode' => (string) $customer->getZip()
                ]
            ]
        ];
        try {
            $return = $client->searchAccounts($request);
        } catch (\SoapFault $fault) {
          ...
        }

我希望请求的格式是这样的。

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://com/dom/ec/services" xmlns:java="java:com.dom.ec.beans">
    <soapenv:Header/>
    <soapenv:Body>
        <ser:searchAccounts>
            <ser:dataRequest>
                <java:UserId>***</java:UserId>
                <java:AccountNumber>00000000</java:AccountNumber>
                <java:PremiseAddress>
                    <java:ZipCode>00000</java:ZipCode>
                </java:PremiseAddress>
            </ser:dataRequest>
        </ser:searchAccounts>
    </soapenv:Body>
</soapenv:Envelope>

而不是像这样的格式 It instead or formatted like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://cso.dom.com/ecservices/">
    <SOAP-ENV:Body>
        <ns1:SearchAccounts>
            <ns1:dataRequest>
                <item>
                    <key>UserId</key>
                    <value>***</value>
                </item>
                <item>
                    <key>AccountNumber</key>
                    <value>0000000000</value>
                </item>
                <item>
                    <key>PremiseAddress</key>
                    <value>
                        <item>
                            <key>ZipCode</key>
                            <value>00000</value>
                        </item>
                    </value>
                </item>
            </ns1:dataRequest>
        </ns1:SearchAccounts>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我是不是漏了一个选项?

解决方案:

我意识到我缺少的是明智地使用SoapVar,以及使用StdClass而不是关联数组。

最终的代码是这样的。

$client = new \SoapClient($this->wsdlURI, [
    'trace' => 1,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'soap_version' => 'SOAP_1_2',
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS|SOAP_USE_XSI_ARRAY_TYPE
]);
$premiseAddress = new \stdClass();
$premiseAddress->ZipCode = (string) $customer->getZip();
$dataRequest = new \stdClass();
$dataRequest->AccountNumber = (string) $customer->getAccountNumber();
$dataRequest->CompanyNumber = '04';
$dataRequest->UserId = 'EFI';
$dataRequest->PremiseAddress = new \SoapVar($premiseAddress, SOAP_ENC_OBJECT);
$searchRequest = new \stdClass();
$searchRequest->dataRequest = new \SoapVar($dataRequest, SOAP_ENC_OBJECT);

给TA打赏
共{{data.count}}人
人已打赏
未分类

PHP 接下来的Y分钟内,每X分钟的日期和时间是什么?

2022-9-9 3:40:18

未分类

Python添加一个循环浏览字段的函数

2022-9-9 3:40:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索