捕获SOAP的错误?

时间:2012-01-12 21:08:30

标签: php soap

如何从SOAP中捕获错误?

如何避免致命错误并将其转换为我自己的错误..在此示例中,它是一个SERVER_BUSY

class Validate_vatno {
    private $client = null;

    private $options = array(
        'debug' => false
        );

    public function __construct(){
        $this->client = new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', array('trace' => true));
    }

    public function check($country, $vatno){
        $result = $this->client->checkVat(array(
            'countryCode' => $country,
            'vatNumber' => $vatno
            ));

        if(!empty($this->options['debug'])){
            echo '<pre>'.htmlentities($this->client->__getLastResponse()).'</pre>';
        }

        if($result->valid){
            list($denomination, $name) = explode(' ', $result->name, 2);

            return array(
                'denomination' => utf8_decode($denomination),
                'name' => ucwords(utf8_decode($name)),
                'address' => ucwords(utf8_decode($result->address)),
                );
        }
        else{
            return array();
        }
    }
}
$vatValidation = new Validate_vatno();

if($return = $vatValidation->check('DK', 33214944)){
    echo '<h1>valid one!</h1>';
    echo 'denomination: ' . $return['denomination']. '<br/>';
    echo 'name: ' . $return['name']. '<br/>';
    echo 'address: ' . $return['address']. '<br/>';
}
else{
    echo '<h1>Invalid VAT</h1>';
}

错误

Fatal error: Uncaught SoapFault exception: [soapenv:Server] { 'SERVER_BUSY' } in /var/www/

1 个答案:

答案 0 :(得分:5)

查看如何处理exceptions

投掷和捕获例外

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>