简体   繁体   English

将 die 更改为不停止执行代码

[英]Change die to not stop code from executing

In the code below, the code would stop from executing further when Kiyoh would not be reachable.在下面的代码中,当 Kiyoh 无法访问时,代码将停止进一步执行。 This is not good for production.这对生产不利。 So I was wondering what the best way would be to replace the die function, in a way that the content would execute further -even when Kiyoh would not be reachable-.所以我想知道最好的方法是替换die函数,以一种内容会进一步执行的方式 - 即使 Kiyoh 无法访问 - 。

The code looks like:代码如下:

        <?php
        // Get KiyOh rating
        $readdir = $_SERVER['DOCUMENT_ROOT'] . '/kiyoh/';
        $file = 'kiyohdata.dat';

        // Open the file to get existing content
        $kiyohdata = file_get_contents($readdir . $file);

        if( $kiyohdata === false ) { // NOT CACHED

            $xml = simplexml_load_file('https://www.kiyoh.nl/widgetfeed.php?company=YYYYY') or die("Error: Cannot create object");
            $kiyohdata = explode(",", $xml->channel->description);

            if(!empty($kiyohdata)) {
                file_put_contents($readdir . $file, serialize($kiyohdata));
            }

        } else { // IS CACHED
            $kiyohdata = unserialize($kiyohdata);
        }

        $cijfer = str_replace('Average score ', '', $kiyohdata[0]);
        $cijfer = str_replace('.', ',', $cijfer);
        $aantal = str_replace(' Total reviews ', '', $kiyohdata[2]);

        ?>

I tried with placing an exception: throw new Exception("Kiyoh is not available at the moment");我尝试放置一个异常: throw new Exception("Kiyoh is not available at the moment"); , but the page crashes anyway (Magento 1.9.4.3 webshop). ,但页面还是崩溃了(Magento 1.9.4.3 网上商店)。

this wil send you the error per email这将通过电子邮件向您发送错误

    <?php
    $to_email_address = webmaster@example.com

    // Get KiyOh rating
    $readdir = $_SERVER['DOCUMENT_ROOT'] . '/kiyoh/';
    $file = 'kiyohdata.dat';


    // Open the file to get existing content
    $kiyohdata = file_get_contents($readdir . $file);

    if( $kiyohdata === false ) { // NOT CACHED

        $xml = simplexml_load_file('https://www.kiyoh.nl/widgetfeed.php?company=YYYYY') or mail($to_email_address,"GOOGOO hapened !","Error: Cannot create object");
        $kiyohdata = explode(",", $xml->channel->description);

        if(!empty($kiyohdata)) {
            file_put_contents($readdir . $file, serialize($kiyohdata));
        }

    } else { // IS CACHED
        $kiyohdata = unserialize($kiyohdata);
    }

    $cijfer = str_replace('Average score ', '', $kiyohdata[0]);
    $cijfer = str_replace('.', ',', $cijfer);
    $aantal = str_replace(' Total reviews ', '', $kiyohdata[2]);

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM