从 SOAP 捕获错误?

发布于 2024-12-26 17:20:19 字数 1691 浏览 0 评论 0原文

如何从 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/

How to catch an error from a SOAP?

How can I avoid the fatal error and convert it to my own error.. In this example it's a SERVER_BUSY

code

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>';
}

error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2025-01-02 17:20:19

查看如何处理异常

抛出和捕获异常

<?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';
?>

Review how to handle exceptions

Throwing and Catching a Exception

<?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';
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文