PHP Conference Japan 2024

SoapFault 類別

(PHP 5, PHP 7, PHP 8)

簡介

表示 SOAP 錯誤。

類別概要

類別 SoapFault 繼承自 Exception {
/* 屬性 */
公開 ?字串 $faultcode = null;
公開 ?字串 $faultcodens = null;
公開 ?字串 $faultactor = null;
公開 混合 $detail = null;
公開 ?字串 $_name = null;
公開 混合 $headerfault = null;
/* 繼承的屬性 */
保護 字串 $message = "";
私有 字串 $string = "";
保護 整數 $code;
保護 字串 $file = "";
保護 整數 $line;
私有 陣列 $trace = [];
私有 ?Throwable $previous = null;
/* 方法 */
公開 __construct(
    陣列|字串|null $code,
    字串 $string,
    ?字串 $actor = null,
    混合 $details = null,
    ?字串 $name = null,
    混合型別 (mixed) $headerFault = null
)
公開 (public) __toString(): 字串 (string)
/* 繼承的方法 */
最終 (final) 公開 (public) Exception::getMessage(): 字串 (string)
最終 (final) 公開 (public) Exception::getPrevious(): ?Throwable
最終 (final) 公開 (public) Exception::getCode(): 整數 (int)
最終 (final) 公開 (public) Exception::getFile(): 字串 (string)
最終 (final) 公開 (public) Exception::getLine(): 整數 (int)
最終 (final) 公開 (public) Exception::getTrace(): 陣列 (array)
最終 (final) 公開 (public) Exception::getTraceAsString(): 字串 (string)
私有 (private) Exception::__clone(): 無 (void)
}

屬性

_name

detail

faultactor

faultcode

faultcodens

faultstring

headerfault

目錄

新增註記

使用者貢獻的註記 3 則註記

dmitry dot koterov at gmail dot com
15 年前
您可以使用未記載且不可見的屬性 $e->faultcode 來存取 $code 的字串版本。因為標準的 $e->getCode() 無法作用。

<?php
$e
= new SoapFault("test", "msg");
var_dump($e->getCode()); // 顯示 "0"
var_dump($e->faultcode); // 顯示 "test"
?>

您也可以使用命名空間的錯誤碼

<?php
$e
= new SoapFault(array("namespace", "test"), "msg");
?>

- 參考 ext/soap/soap.php,PHP_METHOD(SoapFault, SoapFault)。要存取命名空間,請使用 $e->faultcodens
chris AT cmbuckley DOT co DOT uk
14 年前
進一步探究 ext/soap/soap.c 和 set_soap_fault 函式,揭示了建構函式中其他未記載的屬性

<?php
try {
throw new
SoapFault('code', 'string', 'actor', 'detail', 'name', 'header');
} catch (
Exception $ex) {
var_dump($ex->faultcode, $ex->faultstring, $ex->faultactor, $ex->detail, $ex->_name, $ex->headerfault);
}
?>
fbernoldi at gmail dot com
12 年前
大家好,

我決定發布這篇文章,因為它可能會有幫助,我花了好幾天嘗試這樣做。

為了使用具有複雜類型的 WSDL 指定錯誤,例如

WSDL 定義

(xsd:schema 命名空間, ns1 = 目標命名空間)

<xsd:element name="doubleFault">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="detail1" type="xsd:string"/>
<xsd:element name="detail2" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

WSDL 訊息

<message name="fault_specified">
<part name="relevant_name" element="ns1:doubleFault"/>
</message>

WSDL 連接埠類型

<portType name="test">
<operation name="operationTest">
<input message="ns1:not_relevant_request"/>
<output message="ns1:not_relevant_response"/>
<fault name="FaultSpecified" message="ns1:fault_specified"/>
....
</portType>

您必須在 detail 參數中將回應指定為與標籤名稱相對應的陣列。

PHP 程式碼

<?php

函式 operationTest($request_param ...) {

// ...
$array_details = 陣列("detail1" => "說明 1", "detail2" => "說明 2");

返回 new
SoapFault("Server", "範例錯誤字串", null, $array_details, "FaultSpecified");

}

$server = new SOAPServer("handmade.wsdl");
$server->addFunction("operationTest");
$server->handle();

?>

應該會回應如下內容

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mynamespace">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>範例錯誤字串</faultstring>
<detail>
<ns1:doubleFault>
<detail1>說明 1</detail1>
<detail2>說明 2</detail2>
</ns1:doubleFault>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

希望有幫助,
Federico.
To Top