我使用laravel框架,我想检查与Soap服务器的连接是否成功,没有应用程序死于致命错误。
这两个:
$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"
由于
答案 0 :(得分:3)
我今天也在努力解决这个问题。问题是Laravel错误处理程序将此可捕获错误解释为致命错误,并因此中止程序。
要解决此问题,您需要在Laravel的内部错误处理程序之前拦截错误。此方法因Laravel版本而异:
Laravel 4。*
globals.php
文件。这应该在您的app\start\
文件夹中。添加以下代码(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);
if(strpos($e->getMessage(), 'SOAP-ERROR') !== false)
{
return false;
}
这将从错误处理程序中删除SoapFault错误类型。记得在Laravel赢了之后赶上SoapFault!
答案 1 :(得分:1)
这就是我在Laravel 5.1中使用肥皂的方法
clean install laravel 5.1
创建一个控制器SoapController.php
<?php
namespace App\Http\Controllers;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
class SoapController extends Controller {
public function demo()
{
// Add a new service to the wrapper
SoapWrapper::add(function ($service) {
$service
->name('currency')
->wsdl('http://currencyconverter.kowabunga.net/converter.asmx?WSDL')
->trace(true);
});
$data = [
'CurrencyFrom' => 'USD',
'CurrencyTo' => 'EUR',
'RateDate' => '2014-06-05',
'Amount' => '1000'
];
// Using the added service
SoapWrapper::service('currency', function ($service) use ($data) {
var_dump($service->getFunctions());
var_dump($service->call('GetConversionAmount', [$data])->GetConversionAmountResult);
});
}
}
在routes.php
中创建路线 Route::get('/demo', ['as' => 'demo', 'uses' => 'SoapController@demo']);
答案 2 :(得分:1)
试试这个:
try {
$this->client = @new SoapClient("http://some.url/test.wsdl");
$this->session = $this->client->login("username", "password");
} catch (\Throwable $e) {
return "Error";
}
答案 3 :(得分:1)
解决方案是实际要求Soap客户端抛出SoapFault而不是报告E_ERROR。
当Soap客户端报告E_ERROR时,您无需捕获任何内容。
要修复此初始化,请使用以下方法来填充SoapClient:
$clientOptions = array(
'exceptions' => true,
);
try {
$client = new \SoapClient("foo.wsdl", $clientOptions);
} catch (\SoapFault $e) {
// Do what you need to do!;
}
try {
$result = $client->__soapCall($method, $data);
} catch (\SoapFault $e) {
// Do what you need to do!;
}
答案 4 :(得分:0)
@Adam Link提供了一个很好的提示,但在Laravel 5.1中,它似乎不再是AppServiceProvider中的渲染方法。
相反,它已被移至app \ Exceptions \ Handler.php