【发布时间】: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 个错误:
- 如果我的 XML 结构不存在(响应的 xml 名称错误)
$bill= $service->call('getBill', [$data])->billStructure
- 如果我在 $bill= $service->call('getBill', [$data])->billStructure 中有错误的服务名称
我无法捕捉到的错误是:
如果我的 WSDL 链接错误(不正确),那么我会得到 p>
Service.php 第 356 行中的 FatalErrorException: SOAP-ERROR:解析 WSDL:无法从“链接”加载:无法加载外部实体“链接”
所以我的问题是如何处理该异常,以便我可以向我的应用程序的用户提供消息(获取数据时出现问题,请尝试联系管理员...)?
在我的代码中,您可以看到我尝试了某种 Try-Catch 但没有奏效,所以任何建议都会很好。
【问题讨论】:
标签: php soap wsdl laravel-5.1