我正在针对Sonos的Music API(SMAPI)构建服务。有时我必须以以下格式发送回响应:
<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>
我正在使用PHP SOAP库构建服务,对于以上响应,我尝试抛出SoapFault
如下所示:
throw new SoapFault('Client.NOT_LINKED_RETRY', 'Link Code not found retry...');
但是,当我尝试这样做时,发送回的响应如下所示:
<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>
请注意,没有ExceptionInfo
,那SonosError
是空的。是否可以设置ExceptionInfo
和SonosError
使用SoapFault
?我尝试了各种方法,但无法使其正常工作,因此,作为一项工作,我现在正在这样做:
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;
不确定是否相关,但可以在此处找到WSDL 。
更新:当我尝试如下建议时:
$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
);
我得到:
<detail>
<customFault>
<SonosError>5</SonosError>
<ExceptionInfo>NOT_LINKED_RETRY</ExceptionInfo>
</customFault>
</detail>
除了<customFault>
标签,这几乎是我所需要的。有没有办法摆脱它,直接拥有SonosError
和拥有?ExceptionInfo
<detail>
您没有看到该ExceptionInfo
标签的事实是因为在wsdl中未定义该标签。另一方面SonosError
定义。首先,为了填充,SonosError
您必须传递参数。
从这里您可以看到构造函数具有更多参数
SoapFault('code', 'string', 'actor', 'detail', 'name', 'header');
为了通过这样的SonosError
电话
$detail = new StdClass();
$detail->SonosError = 5;
throw new SoapFault('Client.NOT_LINKED_RETRY', 'Link Code not found retry...', null, $details);
至于ExceptionInfo
,必须更改wsdl。就像现在一样,details
标签由该部分表示
<wsdl:message name="customFault">
<wsdl:part name="customFault" element="tns:SonosError"/>
</wsdl:message>
<xs:element name="SonosError" type="xs:int"/>
如果您对这些内容进行更改,则将拥有所需的内容。
<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>
当然,您添加参数,数组将变成这样
$detail = new StdClass();
$detail->SonosError = 5;
$detail->ExceptionInfo = 'NOT_LINKED_RETRY';
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句