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

时间:2015-01-09 09:45:02

标签: php 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

我想要的: 将警告消息替换为:&#34;此服务暂时不可用&#34;

2 个答案:

答案 0 :(得分:1)

您可以在手册中阅读simplexml_load_string功能:

返回类SimpleXMLElement的对象,其属性包含xml文档中保存的数据,或者失败时为FALSE。

因此,只需检查它是否失败以及它是否确实回应了您的此服务暂时不可用

要解除此警告,请考虑生产中的ini_set('display_errors', '0');

答案 1 :(得分:1)

$xml = simplexml_load_string ( $response );

检查是否

$xml === false

并相应地设置错误消息

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