我试图找到一种在CodeIgniter模型中捕捉SOAP异常的方法,我搜索了几个小时,发现更多的人试图得到答案,但没有成功。
在创建SoapClient的新实例时,如何捕捉异常?我尝试了以下方法,但codeIgniter仍然会给我"遇到PHP错误"页面,在这个项目中,这在大多数情况下都很好。
class User_model extends CI_Model
{
public function __construct()
{
$this->WAPI_service_url = 'http://api.lan/WAPITranslator/WAPITranslator.asmx?wsdl';
try{
$this->connection = new SoapClient($this->WAPI_service_url, array('trace' => 1, "exception" => 0));
}
catch(Exception $e){
return false;
}
}
遵循PHP文档,我会这样做:
class User_model extends CI_Model
{
public function __construct() {
$this->WAPI_service_url = 'http://api.lan/WAPITranslator/WAPITranslator.asmx?wsdl';
try {
$this->connection = new SoapClient($this->WAPI_service_url, array('trace' => 1, "exceptions" => true));
} catch (Exception $e){
$e->getMessage(); // get the error message and save it somewhere
$e->getCode(); // gets the error code
}
}
}