我正在进行一个项目,在该项目中,我使用SOAP服务验证来自用户的信息。我目前正在处理错误,假设我收到了来自web服务的响应,但也需要处理服务超时或不可用的边缘情况。
在超时或服务不可用的情况下,我需要假装请求是成功的( web服务批准了信息),但我不清楚抛出了哪些异常。
一些伪码:
// $client is PHP's SoapClient class
try {
$response = $client->SomeSoapRequest();
}
catch(SoapFault $e){
// handle issues returned by the web service
}
catch(Exception $e){
// handle PHP issues with the request
}
但我似乎找不到:
SoapFault
吗?如果是,那么区分超时错误和web服务问题(比如类型错误等)的最佳方法是什么?我发现有一个页面提到了一个错误,其中的消息是“错误加载标题”的结果,但是没有提到这是否是Soap错误。发布于 2013-01-11 22:18:10
1)在超时的情况下,PHP抛出带有faultcode="HTTP"
和faultstring="Error Fetching http headers"
的faultcode="HTTP"
异常。
2)在我看来,区分超时错误和web服务问题的最佳方法是查看faultcode
和SoapFault类的faultstring
成员。
特别是,faultcode
元素用于软件来提供识别故障的算法机制。
因为您也可以在PHP手册注释中读取,所以没有读取faultcode
属性的方法,所以您必须直接访问它(例如。$e->faultcode
),因为getCode()
方法不工作。
SOAP1.1规格为faultcode
字段定义了四个可能的值:
除了这些代码之外,PHP还使用HTTP
代码来识别在协议级别上发生的错误(例如:套接字错误);例如,如果在http.c源代码中搜索add_soap_fault
,则可以在生成某些此类错误时看到这些错误。
通过在PHP扩展源文件中搜索add_soap_fault
和soap_server_fault
函数,我构建了以下PHP SoapFault
异常列表:
HTTP
----
Unable to parse URL
Unknown protocol. Only http and https are allowed.
SSL support is not available in this build
Could not connect to host
Failed Sending HTTP SOAP request
Failed to create stream??
Error Fetching http headers
Error Fetching http body: No Content-Length: connection closed or chunked data
Redirection limit reached: aborting
Didn't recieve an xml document
Unknown Content-Encoding
Can't uncompress compressed response
Error build soap request
VersionMismatch
---------------
Wrong Version
Client
------
A SOAP 1.2 envelope can contain only Header and Body
A SOAP Body element cannot have non Namespace qualified attributes
A SOAP Envelope element cannot have non Namespace qualified attributes
A SOAP Header element cannot have non Namespace qualified attributes
Bad Request
Body must be present in a SOAP envelope
Can't find response data
DTD are not supported by SOAP
encodingStyle cannot be specified on the Body
encodingStyle cannot be specified on the Envelope
encodingStyle cannot be specified on the Header
Error cannot find parameter
Error could not find "location" property
Error finding "uri" property
looks like we got "Body" with several functions call
looks like we got "Body" without function call
looks like we got no XML document
looks like we got XML without "Envelope" element
Missing parameter
mustUnderstand value is not boolean
SoapClient::__doRequest() failed
SoapClient::__doRequest() returned non string value
Unknown Data Encoding Style
Unknown Error
DataEncodingUnknown
MustUnderstand
--------------
Header not understood
Server
------
Couldn't find WSDL
DTD are not supported by SOAP
Unknown SOAP version
WSDL generation is not supported yet
3)要模拟超时条件,请尝试使用以下代码:
soapclient.php
<?php
ini_set('default_socket_timeout', 10);
$client = new SoapClient(null,
array(
'location' => "http://localhost/soapserver.php",
'uri' => "http://localhost/soapserver.php",
'trace' => 1
)
);
try {
echo $return = $client->__soapCall("add",array(41, 51));
} catch (SoapFault $e) {
echo "<pre>SoapFault: ".print_r($e, true)."</pre>\n";
//echo "<pre>faultcode: '".$e->faultcode."'</pre>";
//echo "<pre>faultstring: '".$e->getMessage()."'</pre>";
}
?>
soapserver.php
<?php
function add($a, $b) {
return $a + $b;
}
sleep(20);
$soap = new SoapServer(null, array('uri' => 'http://localhost/soapserver.php'));
$soap->addFunction("add");
$soap->handle();
?>
注意,sleep
调用在SoapServer.php
脚本中的时间(20)比为SoapClient.php
脚本中的default_socket_timeout
参数指定的时间(10)长。
如果您想要模拟服务不可用性,例如,您可以在location
脚本中将http
协议更改为https
,假设您的web服务器不是为SSL配置的;这样做,PHP应该抛出一个“无法连接到主机”SoapFault。
发布于 2010-09-08 19:41:01
在HTTPS上进行SOAP调用时,似乎没有考虑到default_socket_timeout
:
错误在编写时打开。正如在删除的答案http://www.darqbyte.com/2009/10/21/timing-out-php-soap-calls/中引用的博客文章Robert的评论所指出的那样,本文讨论的解决方法(用curl请求覆盖SoapClient::__doRequest()
)也可以解决这个问题。
另一个相关的错误是:
在博客文章中提到的代码经历了一些更改,可以在支持HTTP身份验证的最新形式中找到:
无论如何,不应该再需要解决方法了,因为这个问题已经在PHP扩展中解决了。
发布于 2009-05-07 16:53:39
处理服务中的超时
$client = new SoapClient($wsdl, array("connection_timeout"=>10));
// SET SOCKET TIMEOUT
if(defined('RESPONSE_TIMEOUT') && RESPONSE_TIMEOUT != '') {
ini_set('default_socket_timeout', RESPONSE_TIMEOUT);
}
https://stackoverflow.com/questions/835184
复制相似问题