使用 PHP SoapClient 时如何将 nil="true" 属性添加到 SoapVar?
How can I add the nil="true" attribute to a SoapVar when using PHP SoapClient?
以下是我为 Soap 调用设置参数的方式:
$params = array(
"connectionToken" => $this->token,
"inboxName" => $this->inboxName
);
$wrapper = new \stdClass();
$typedVar = new \SoapVar($value, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
$wrapper->anyType = $typedVar;
$params["fnParameterValues"] = $wrapper;
这为请求创建了正确的 XML 结构,除了如果 $value = null 那么我需要向 nil="true" 的 anyType 节点添加一个属性(实际上更准确:- xsi:nil="true")。我怎样才能做到这一点?
我知道有点晚了,但我遇到了同样的问题,我找到的解决方案可能对其他人有帮助。
如果您正在使用 WSDL,内置 PHP SoapServer 应该自动将此属性添加到具有 null
值的任何元素,该元素定义为nillable
,以适应 WSDL 定义的元素。
如果您需要手动添加属性,唯一的方法似乎是捕获输出并修改 XML 响应。
您可以使用 Zend SoapServer 包装器 (https://docs.zendframework.com/zend-soap/server/) and post-process the response (https://docs.zendframework.com/zend-soap/server/#response-post-processing),像这样:
// Get a response as a return value of handle(),
// instead of emitting it to standard output:
$server->setReturnResponse(true);
$response = $server->handle();
if ($response instanceof SoapFault) {
// Manage exception
/* ... */
} else {
// Post-process response and return it
// I. e., load response as XML document, add attribute to node...
/* ... */
}
以下是我为 Soap 调用设置参数的方式:
$params = array(
"connectionToken" => $this->token,
"inboxName" => $this->inboxName
);
$wrapper = new \stdClass();
$typedVar = new \SoapVar($value, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
$wrapper->anyType = $typedVar;
$params["fnParameterValues"] = $wrapper;
这为请求创建了正确的 XML 结构,除了如果 $value = null 那么我需要向 nil="true" 的 anyType 节点添加一个属性(实际上更准确:- xsi:nil="true")。我怎样才能做到这一点?
我知道有点晚了,但我遇到了同样的问题,我找到的解决方案可能对其他人有帮助。
如果您正在使用 WSDL,内置 PHP SoapServer 应该自动将此属性添加到具有 null
值的任何元素,该元素定义为nillable
,以适应 WSDL 定义的元素。
如果您需要手动添加属性,唯一的方法似乎是捕获输出并修改 XML 响应。
您可以使用 Zend SoapServer 包装器 (https://docs.zendframework.com/zend-soap/server/) and post-process the response (https://docs.zendframework.com/zend-soap/server/#response-post-processing),像这样:
// Get a response as a return value of handle(),
// instead of emitting it to standard output:
$server->setReturnResponse(true);
$response = $server->handle();
if ($response instanceof SoapFault) {
// Manage exception
/* ... */
} else {
// Post-process response and return it
// I. e., load response as XML document, add attribute to node...
/* ... */
}