我制作了一个使用SOAP交换从web API获取数据的web应用程序。这最初是以程序的方式完成的,现在我正试图将其转移到Laravel框架中。我有一个view
设置显示给用户,如果SOAP响应是"请求被节流服务器拒绝",但我不知道如何检查特定的错误。下面是类:
<?php namespace App'Models;
use SoapClient;
use Illuminate'Http'RedirectResponse;
class SoapWrapper {
public function soapExchange() {
// set WSDL for authentication and create new SOAP client
$auth_url = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";
// set WSDL for search and create new SOAP client
$search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";
// array options are temporary and used to track request & response data
$auth_client = @new SoapClient($auth_url);
// array options are temporary and used to track request & response data
$search_client = @new SoapClient($search_url);
// run 'authenticate' method and store as variable
$auth_response = $auth_client->authenticate();
// call 'setCookie' method on '$search_client' storing SID (Session ID) as the response (value) given from the 'authenticate' method
// check if an SID has been set, if not it means Throttle server has stopped the query, therefore display error message
if (isset($auth_response->return)) {
$search_client->__setCookie('SID',$auth_response->return);
} else {
return Redirect::route('throttle');
}
}
}
问题是,它抛出"请求被节流服务器拒绝"默认Laravel错误在$auth_response = $auth_client->authenticate();
之前,它得到if
语句,检查是否有一个值(SessionID)已由SOAP请求返回。由于某些原因,当它以程序方式设置时,它没有这样做。
if
语句检查一个值是否已经从authenticate()
方法返回,如果有,将它(SessionID)分配给搜索客户端的cookie以授权搜索。否则将显示自定义错误消息。
我试过使用is_soap_fault
,但这并没有抓住它,因为它不是技术上的肥皂故障。我还尝试删除导致问题的行,并将if
语句更改为:
if (isset($auth_client->authenticate()->return) {...
但是这也会导致默认的Laravel SoapFault页面。return Redirect::route('throttle')
向用户显示一个自定义的错误页面,保存为throttle.blade.php
。
谁知道我如何测试油门错误?
没关系,在这里找到了答案:在正确创建新的SoapClient时捕获异常。
我还是会把修改后的代码贴出来,以防将来对别人有用:
<?php namespace App'Models;
use SoapClient;
use Illuminate'Http'RedirectResponse;
class SoapWrapper {
public function soapExchange() {
try {
// set WSDL for authentication and create new SOAP client
$auth_url = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";
// set WSDL for search and create new SOAP client
$search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";
// array options are temporary and used to track request & response data
$auth_client = @new SoapClient($auth_url);
// array options are temporary and used to track request & response data
$search_client = @new SoapClient($search_url);
// run 'authenticate' method and store as variable
$auth_response = $auth_client->authenticate();
// add SID (SessionID) returned from authenticate() to cookie of search client
$search_client->__setCookie('SID', $auth_response->return);
} catch ('SoapFault $e) {
return Redirect::route('throttle');
}
}
}