2014-09-19 23 views
0

我使用laravel框架,並且我想檢查與Soap服務器的連接是否成功,沒有應用程序因致命錯誤而死亡。Laravel測試連接到SOAP WDSL和異常處理

兩個這樣的:

$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" 

感謝

回答

3

我這個問題今天掙扎以及。問題是Laravel錯誤處理程序正在將此可捕獲錯誤解釋爲致命錯誤,並因此而終止程序。

爲了解決這個問題,您需要在Laravel的內部錯誤處理程序之前攔截錯誤。這種方法取決於你的Laravel版本:

Laravel 4 *

  1. 轉到您的globals.php文件。這應該在您的app\start\文件夾中。
  2. 添加以下代碼(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. *

  1. 沒有globals.php文件。所有IoC電話均通過ServiceProviders進行處理。去app\Providers\AppServiceProvider.php
  2. 找到render()函數。
  3. return parent::render($request, $e);

    if(strpos($e->getMessage(), 'SOAP-ERROR') !== false) 
    { 
        return false; 
    } 
    

這添加以下代碼將刪除您的錯誤處理程序中的SOAPFault錯誤類型。記得趕上SoapFault,因爲Laravel不會!

0

@Adam鏈接提供了一個很好的提示,但在Laravel 5.1中,似乎看起來不再有AppServiceProvider中的呈現方法。

相反,它已被移動到App \例外\ Handler.php

1

我這是怎麼了肥皂Laravel 5.1工作

  1. 乾淨的安裝laravel 5.1

  2. 安裝artisaninweb/laravel-soap

  3. 創建一個控制器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); 
        }); 
    } 
    
    } 
    
  4. 在routes.php文件創建路線

    Route::get('/demo', ['as' => 'demo', 'uses' => '[email protected]']);

1

試試這個:

try { 
$this->client = @new SoapClient("http://some.url/test.wsdl"); 
$this->session = $this->client->login("username", "password"); 
} catch (\Throwable $e) { 
    return "Error"; 
}