[英]How can I add custom elements to the detail section of a SoapFault using PHP's SOAP library
I am building a service against Sonos' Music API (SMAPI) . 我正在针对Sonos的Music API(SMAPI)构建服务。 Sometimes I have to send back a response in the following format:
有时我必须以以下格式发送回响应:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>Client.NOT_LINKED_RETRY</faultcode>
<faultstring>Link Code not found retry...</faultstring>
<detail>
<ExceptionInfo>NOT_LINKED_RETRY</ExceptionInfo>
<SonosError>5</SonosError>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
I am building my service using the PHP SOAP library and for the above response I tried throwing a SoapFault
like this: 我正在使用PHP SOAP库构建服务,对于以上响应,我尝试抛出如下
SoapFault
:
throw new SoapFault('Client.NOT_LINKED_RETRY', 'Link Code not found retry...');
But when I try this the response that is sent back looks like this: 但是,当我尝试此操作时,发送回的响应如下所示:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>Client.NOT_LINKED_RETRY</faultcode>
<faultstring>Link Code not found retry...</faultstring>
<detail>
<SonosError/>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Notice that there is no ExceptionInfo
and that SonosError
is empty. 请注意,没有
ExceptionInfo
并且SonosError
为空。 Is it possible to set ExceptionInfo
and SonosError
using SoapFault
? 是否可以使用
SoapFault
设置ExceptionInfo
和SonosError
? I tried all kinds of things, but couldn't get it working, so as a work around I am doing this now: 我尝试了各种方法,但无法使其正常工作,因此,作为一项工作,我现在正在这样做:
http_response_code(500);
header("Content-type: text/xml");
$ret = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$ret .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
$ret .= '<SOAP-ENV:Body>';
$ret .= '<SOAP-ENV:Fault>';
$ret .= '<faultcode>Client.NOT_LINKED_RETRY</faultcode>';
$ret .= '<faultstring>Link Code not found retry...</faultstring>';
$ret .= '<detail>';
$ret .= '<ExceptionInfo>NOT_LINKED_RETRY</ExceptionInfo>';
$ret .= '<SonosError>5</SonosError>';
$ret .= '</detail>';
$ret .= '</SOAP-ENV:Fault>';
$ret .= '</SOAP-ENV:Body>';
$ret .= '</SOAP-ENV:Envelope>'."\n";
echo $ret; exit;
Not sure if it's relevant but the WSDL can be found here . 不确定是否相关,但可以在此处找到WSDL。
Update: when I try the suggestion below like this: 更新:当我尝试如下建议时:
$detail = new StdClass();
$detail->SonosError = 5;
$detail->ExceptionInfo = 'NOT_LINKED_RETRY';
throw new SoapFault(
'Client.NOT_LINKED_RETRY',
'Link Code not found retry...',
NULL,
$detail
);
I get: 我得到:
<detail>
<customFault>
<SonosError>5</SonosError>
<ExceptionInfo>NOT_LINKED_RETRY</ExceptionInfo>
</customFault>
</detail>
This is almost what I need, except for <customFault>
tag. 除了
<customFault>
标记外,这几乎是我需要的。 Is there a way to get rid of it and have SonosError
and ExceptionInfo
in <detail>
directly? 有没有办法摆脱它,并直接在
<detail>
包含SonosError
和ExceptionInfo
?
The fact that you don't see the ExceptionInfo
tag is because it is not defined in the wsdl. 您没有看到
ExceptionInfo
标记的事实是因为未在wsdl中定义它。 On the other hand SonosError
is defined. 另一方面,定义了
SonosError
。 First thing first, in order to fill the SonosError
you have to pass the arguments. 首先,为了填充
SonosError
您必须传递参数。
From here you can see that the constructor has more parameters 从这里您可以看到构造函数具有更多参数
SoapFault('code', 'string', 'actor', 'detail', 'name', 'header');
In order to pass the SonosError
call it like this 为了传递
SonosError
像这样调用它
$detail = new StdClass();
$detail->SonosError = 5;
throw new SoapFault('Client.NOT_LINKED_RETRY', 'Link Code not found retry...', null, $details);
As for the ExceptionInfo
, the wsdl must be changed. 至于
ExceptionInfo
,必须更改wsdl。 As it is now, the details
tag is represented by this sections 现在,该部分表示了
details
标签
<wsdl:message name="customFault">
<wsdl:part name="customFault" element="tns:SonosError"/>
</wsdl:message>
<xs:element name="SonosError" type="xs:int"/>
If you change the above sections with these, you will have what you need. 如果您对这些内容进行更改,您将拥有所需的内容。
<wsdl:message name="customFault">
<wsdl:part name="customFault" type="tns:customFaultType" />
</wsdl:message>
<xs:complexType name="customFaultType">
<xs:sequence>
<xs:element name="SonosError" type="xs:int"/>
<xs:element name="ExceptionInfo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
And of course you add the parameter and the array becomes like this 当然,您添加参数,数组将变成这样
$detail = new StdClass();
$detail->SonosError = 5;
$detail->ExceptionInfo = 'NOT_LINKED_RETRY';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.