gpt4 book ai didi

PHP - 在带有 SoapClient 的信封中使用多个 xmlns

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:46 25 4
gpt4 key购买 nike

我正在尝试在 soap 调用信封中使用多个 xmlns 命名空间进行 SOAP 调用,但我无法弄清楚如何正确执行...

这是我现在的代码:

$soapClient = new SoapClient(WSDL_URL, array(
"trace" => true,
"exceptions" => true
));
$soapClient->__setLocation(WSDL_LOCATION);

$request = '
<ns1:someNodeName xmlns="http://some.webservice.url.com">
<ns2:someOtherNodeName>
// [REQUEST DETAILS]
</ns2:someOtherNodeName>
</ns1:someNodeName>
';

$params = new SoapVar($request, XSD_ANYXML);

try {
$results = $soapClient->someFunctionName($params);
return $results;
}
catch (Exception $e) {
$error_xml = $soapClient->__getLastRequest();
echo $error_xml . "\n";
echo $e->getMessage() . "\n";
}

此代码向我提供如下 XML 请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://some.webservice.url.com">
<SOAP-ENV:Body>
<ns1:someNodeName xmlns="http://some.webservice.url.com">
<ns2:someOtherNodeName>
// [REQUEST DETAILS]
</ns2:someOtherNodeName>
</ns1:someNodeName>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我想改变的是信封线,得到类似的东西:

<SOAP-ENV:Envelope 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://some.webservice.url.com"
xmlns:ns2="http://some.other.webservice.url.com"
>

有什么办法可以实现吗?

最佳答案

XMLNS 属性是命名空间定义,如果您在该命名空间中添加节点,则会添加这些属性。它们不需要在根元素上,而是在使用它的元素或其祖先元素之一上。

$request = '
<ns1:someNodeName
xmlns:ns1="http://some.webservice.url.com"
xmlns:ns2="http://some.other.webservice.url.com">
<ns2:someOtherNodeName>
// [REQUEST DETAILS]
</ns2:someOtherNodeName>
</ns1:someNodeName>
';

如果您动态创建 XML,您应该使用 XML 扩展,如 DOM 或 XMLWriter。他们有特定的方法来创建带有命名空间的元素,并将自动添加定义。

$xmlns = [
'ns1' => "http://some.webservice.url.com",
'ns2' => "http://some.other.webservice.url.com"
];

$document = new DOMDocument();
$outer = $document->appendChild(
$document->createElementNS($xmlns['ns1'], 'ns1:someNodeName')
);
$inner = $outer->appendChild(
$document->createElementNS($xmlns['ns2'], 'ns2:someOtherNodeName')
);
$inner->appendChild(
$document->createComment('[REQUEST DETAILS]')
);

$document->formatOutput = TRUE;
echo $document->saveXml($outer);

输出:

<ns1:someNodeName xmlns:ns1="http://some.webservice.url.com">
<ns2:someOtherNodeName xmlns:ns2="http://some.other.webservice.url.com">
<!--[REQUEST DETAILS]-->
</ns2:someOtherNodeName>
</ns1:someNodeName>

关于PHP - 在带有 SoapClient 的信封中使用多个 xmlns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33209329/

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