gpt4 book ai didi

php - 当传递给 SoapClient 时,关联数组键成为 XML 中的参数

转载 作者:行者123 更新时间:2023-12-04 08:39:13 26 4
gpt4 key购买 nike

我有一个关联数组:

$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 中的 key 。
SOAP 客户端,
    $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>

最佳答案

与 PHP 手册的内容相反 says ,好像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 类(class):
<?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;
}

关于php - 当传递给 SoapClient 时,关联数组键成为 XML 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64647054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com