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

问题描述 投票:1回答:1

奇怪的情况下使用 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>

我是不是漏了一个选项?

php soap soap-client
1个回答
0
投票

我意识到我缺少的是明智地使用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);
© www.soinside.com 2019 - 2024. All rights reserved.