Laravel测试与SOAP WDSL的连接和异常处理 [英] Laravel test connection to SOAP WDSL and exception handling
问题描述
我使用laravel框架,我想检查与Soap服务器的连接是否成功,而应用程序不会因致命错误而死.
I use the laravel framework and I want to check if a connection to Soap server was successful or not, without the app dying with fatal error.
这两者:
$this->client = @new SoapClient("http://some.url/test.wsdl");
$this->session = $this->client->login("username", "password");
if (is_soap_fault($this->session)) {
return "Error";
}
这:
try {
$this->client = @new SoapClient("http://some.url/test.wsdl");
$this->session = $this->client->login("username", "password");
} catch (SoapFault $e) {
return "Error";
}
导致致命错误:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://some.url/test.wsdl' : failed to load external entity "http://some.url/test.wsdl"
谢谢
推荐答案
我今天也在这个问题上苦苦挣扎.问题在于Laravel错误处理程序将这个可捕获的错误解释为致命错误,并因此中止了程序.
I struggled with this issue today as well. The problem is the Laravel error handler is interpreting this catchable error as a fatal error, and aborting the program as a result.
要解决此问题,您需要在Laravel的内部错误处理程序之前拦截错误.此方法取决于您的Laravel版本:
To counter this, you need to intercept the error prior to Laravel's internal error handler. This method varies depending on your Laravel version:
Laravel 4.*
- 转到您的
globals.php
文件.这应该在您的app\start\
文件夹中. -
添加以下代码(感谢dmgfjaved ):
- Go to your
globals.php
file. This should be in yourapp\start\
folder. Add the following code (Thanks dmgfjaved):
App::fatal(function($exception)
{ //If SOAP Error is found, we don't want to FATALLY crash but catch it instead
if(strpos($exception->getMessage(), 'SOAP-ERROR') !== FALSE)
{
return '';
}
});
Laravel 5.*
- 没有
globals.php
文件.所有IoC呼叫均通过ServiceProviders
处理.转到app\Providers\AppServiceProvider.php
. - 找到
render()
函数. -
在
return parent::render($request, $e);
- There is no
globals.php
file. All IoC calls are handled viaServiceProviders
. Go toapp\Providers\AppServiceProvider.php
. - Find the
render()
function. Add the following code before the
return parent::render($request, $e);
if(strpos($e->getMessage(), 'SOAP-ERROR') !== false)
{
return false;
}
这将从您的错误处理程序中删除SoapFault错误类型.记住要抓住SoapFault,因为Laravel不会!
This will remove the SoapFault error type from your error handler. Remember to catch the SoapFault as Laravel won't!
这篇关于Laravel测试与SOAP WDSL的连接和异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!