【问题标题】:How to catch FatalError Exception using SoapWrapper in PhP如何在 PhP 中使用 SoapWrapper 捕获 FatalError 异常
【发布时间】:2015-11-25 10:26:18
【问题描述】:

我在我的应用程序中使用 Laravel SoapClient Wrapper 进行 Soap 调用。我将它用作模型扩展,因此我可以在需要肥皂调用的每个控制器中轻松使用它。到目前为止一切都很好,但我想确保如果 WSDL URL 不正确或 SOAP 服务器上不存在服务调用等,我的应用程序不会崩溃......我试图“尝试捕获”这些代码部分但是我没有成功。我的代码是下一个:

use Artisaninweb\SoapWrapper\Extension\SoapService;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;

class Wsdl extends SoapService{
  protected $name = 'test';
  protected $wsdl;
  protected $trace = true;

  public function __construct($wsdl)
  {
    $this->wsdl = $wsdl;
    SoapWrapper::add(function ($service) {
        $service
            ->name($this->name)
            ->wsdl($this->wsdl)
            ->trace(true);
    });
  }

  public function functions()
  {
    return $this->getFunctions();
  }

  public function bills(Array $data)
  {   //function for get bills for one customer
    $bill= null;
    try{
        SoapWrapper::service($this->name, function ($service) use ($data, &$bill) {
            $bill= $service->call('getBill', [$data])->billStructure;
        });
        return $bill;
    }
    catch(\SoapFault $e){
        //dd("soapFault");
        return null;
    }
    catch(\ErrorException $e){
        //dd("errorException");
        return null;
    }
  }   

}      

在我的控制器中,我这样称呼这个模型

try{
  $soap = new Wsdl('link');   
  $data = [
   'param' => 1
  ];
  $result= $soap->bills($data);             
}
catch(\Exception  $e){
//problem 
  flash()->error("There was a problem with getting data, try to contact administrator");
}
catch(\FatalErrorException  $e){
  //problem 
  flash()->error("There was a problem with getting data, try to contact administrator");
}

我设法发现了 2 个错误:

  1. 如果我的 XML 结构不存在(响应的 xml 名称错误)

$bill= $service->call('getBill', [$data])->billStructure

  1. 如果我在 $bill= $service->call('getBill', [$data])->billStructure
  2. 中有错误的服务名称

我无法捕捉到的错误是:

如果我的 WSDL 链接错误(不正确),那么我会得到 ​​p>

Service.php 第 356 行中的 FatalErrorException: SOAP-ERROR:解析 WSDL:无法从“链接”加载:无法加载外部实体“链接”

所以我的问题是如何处理该异常,以便我可以向我的应用程序的用户提供消息(获取数据时出现问题,请尝试联系管理员...)?

在我的代码中,您可以看到我尝试了某种 Try-Catch 但没有奏效,所以任何建议都会很好。

【问题讨论】:

    标签: php soap wsdl laravel-5.1


    【解决方案1】:

    问题出在 Laravel 框架中。 Laravel 将此错误解释为致命错误并因此中止了我的应用程序。我正在使用 Laravel 5.1。为了完成这项工作,他不得不改变他的一些常规设置。

    在 app\Exceptions 中,您需要添加一些框架忽略 FatalError 异常的代码。默认情况下,函数render() 看起来像这样

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
        return parent::render($request, $e);
    }
    

    要使这个错误不会使您的应用程序崩溃,请添加一个附加条件,然后函数看起来像这样

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
        if(strpos($e->getMessage(), 'SOAP-ERROR') !== false)
        {
            return false;
        }
        return parent::render($request, $e);
    }
    

    我认为在失败了 1 天之后,这是最好的解决方案(当然,如果你使用 Laravel 5.1)。对于纯 php,有几种方法可以做到:

    1. 编写您自己的错误处理程序
    2. 打开和关闭 x_debug
    3. 尝试从不正确的 wsdl 中获取内容,然后以某种方式验证它

    【讨论】:

      【解决方案2】:
      //Could manipulate:
      public function render($request, Exception $e)
          {
              switch (get_class($e)) {
                  case 'SoapFault':
                      return 'Erro do serviço no cliente: '.$e->faultstring;
                      break;
      
                  default:
                      return parent::render($request, $e);
                      break;
              }        
          }
      

      【讨论】:

        猜你喜欢
        • 2017-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 2016-08-20
        相关资源
        最近更新 更多