我有一个关联数组:
$params = [
'trid' => null,
'merchantCode' => null,
'paymentMethod' => null,
'returnUrl' => site_url(null),
'notificationUrl' => site_url(null),
'language' => 'null',
'currency' => 'null',
'isTestMode' => false,
'productsXml' => null,
'needInvoice' => true,(虚无持有现实中的实际价值)
当我检查__getLastRequest时,我会完全丢失生成的XML中的键。
SoapClient,
$soap = new SoapClient(null, $options);
$soap->__soapCall('Request', $params);
$request = $soap->__getLastRequest();请注意,我与端点有一个非WSDL连接。
生成的XML将具有一个<param[keyNum] xsi:type="xsd:typeOfVariable">字段值,而不是来自关联数组(例如<trid> )的实际键值。
<SOAP-ENV:Body><ns1:Request><param0 xsi:type="xsd:string">null</param0><param1 xsi:type="xsd:string">null</param1></Request>发布于 2020-11-06 22:09:46
与PHP手册中的他说相反,arguments似乎从未被解释为一个关联数组。
如果我们尝试这个简单的测试:
<?php
$options = ['location'=>'http://localhost/unknown', 'uri'=>'test', 'trace'=>true];
$soap = new SoapClient(null, $options);
try
{
$soap->__soapCall('Request', ['foo'=>1, 'bar'=>2]);
}
catch(SoapFault $e)
{
header('Content-type: text/xml');
echo $soap->__getLastRequest();
}
?>我们得到:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:Request>
<param0 xsi:type="xsd:int">1</param0>
<param1 xsi:type="xsd:int">2</param1>
</ns1:Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>我们看到参数名被忽略了。
现在,如果我们尝试另一个答案所建议的(将数组包装在另一个数组中):
<?php
$options = ['location'=>'http://localhost/unknown', 'uri'=>'test', 'trace'=>true];
$soap = new SoapClient(null, $options);
try
{
$soap->__soapCall('Request', [['foo'=>1, 'bar'=>2]]);
}
catch(SoapFault $e)
{
header('Content-type: text/xml');
echo $soap->__getLastRequest();
}
?>我们得到:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:Request>
<param0 xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">foo</key>
<value xsi:type="xsd:int">1</value>
</item>
<item>
<key xsi:type="xsd:string">bar</key>
<value xsi:type="xsd:int">2</value>
</item>
</param0>
</ns1:Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>我们看到有一个参数的类型是Map。这不是我们想要的。
解决方案是使用SoapParam类:
<?php
$options = ['location'=>'http://localhost/unknown', 'uri'=>'test', 'trace'=>true];
$soap = new SoapClient(null, $options);
try
{
$soap->__soapCall('Request', [new SoapParam(1, 'foo'), new SoapParam(2, 'bar')]);
}
catch(SoapFault $e)
{
header('Content-type: text/xml');
echo $soap->__getLastRequest();
}
?>现在我们得到了我们想要的:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:Request>
<foo xsi:type="xsd:int">1</foo>
<bar xsi:type="xsd:int">2</bar>
</ns1:Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>请注意,您可以轻松地将关联数组转换为SOAP参数,如下所示:
function getSoapParams($params)
{
$a = [];
foreach($params as $name=>$value)
$a[] = new SoapParam($value, $name);
return $a;
}发布于 2020-11-06 14:23:39
根据文件中的说明
如果要调用__soapCall,则必须将参数包装在另一个数组中,如下所示:
$params = array('username'=>'name', 'password'=>'secret');
<?php
$client->__soapCall('login', array($params));
?>以下一行:
$soap->__soapCall('Request', $params);
应:
$soap->__soapCall('Request', [$params]);
https://stackoverflow.com/questions/64647054
复制相似问题