php - PHP本机soapClient中的SoapRequest格式
问题描述
奇怪的情况使用SoapClient
我有以下代码来触发一个简单的肥皂请求。
$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>
它改为或格式如下:
<?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);
推荐阅读
- php - 从 Live Hosted Server 到本地 XAMPP 的网站备份失败
- android - 在 Android 中以编程方式创建 URL 快捷方式
- flutter - 如何使 SliverGrid 居中?
- python-3.x - 在定义了“with”语句的类中多次尝试使用相同的 SUIT
- node.js - 为什么订阅方法返回订阅者而不是响应值
- css - 如果字符数超过 200,则在引导表中使用省略号
- r - 如何使用 dplyr 获取每行具有最大值的列
- pine-script - Pinescript - 每次计算都应该调用函数
- android - 创建一个团队喜欢动态布局android
- javascript - 使用 MomentJS 从 ISODate 中减去 30 天