PHP - 将警告替换为错误(使用 soap)

PHP - Replace Warning with error (using soap)

我正在使用 SOAP 从某个服务器获取数据(数据来自 .xml)。但有时 SOAP 服务器已关闭,我需要显示一些错误消息而不是 :

Warning: simplexml_load_string(): Entity: line 2: parser error : Start tag expected, '<' not found in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): ^ in /var/www/class/data2.php 

我的代码是:

$client = new SOAPClient ( 'link.wsdl' ); // initiate new SoapClient
$password ['_'] = 'PASSWORD'; // password for authenticate_user function in SoapHeader
$encoded = new SoapVar ( $password, SOAP_ENC_OBJECT ); // make SoapVariable out of $password
$header = new SoapHeader ( 'http://soapinterop.org/echoheader/', 'authenticate_user', $encoded ); // put authenticate_user method, and password in header
$client->__setSoapHeaders ( $header ); // set SoapHeader
$response = $client->get_details ($this->vin); // calling get_details with the vin given in the form field
$xml = simplexml_load_string ( $response ); // converting the response string to xml
$json = json_encode ( $xml ); // converting to an array in to easy steps (step 1)
$array = json_decode ( $json, TRUE ); // step 2

我想要的: 将警告消息替换为类似以下内容:"This service is temporary unavaliable"

您可以在手册中阅读 simplexml_load_string 函数:

Returns class SimpleXMLElement 的对象,其属性包含 xml 文档中保存的数据,失败时为 FALSE。

所以只需检查它是否失败以及它是否响应您的 此服务暂时不可用

要消除此警告,请考虑 ini_set('display_errors', '0'); 生产环境

之后

$xml = simplexml_load_string ( $response );

检查是否

$xml === false

并相应地设置错误信息

http://php.net/manual/en/function.simplexml-load-string.php