PHP simplexml_load_file - 捕获文件错误

发布于 2024-08-14 19:22:10 字数 81 浏览 2 评论 0原文

是否可以捕获 simplexml 文件错误?我正在连接到一个有时会失败的网络服务,如果它返回一些http错误或类似的错误,我需要让系统跳过一个文件。

Is it possible to catch simplexml file errors? I'm connecting to a webservice that sometimes fails, and I need to make the system skip a file if it returns some http error or something similar.

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

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

发布评论

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

评论(7

心碎的声音 2024-08-21 19:22:10

使用 @ 简直就是肮脏的。

如果你看一下手册,有一个选项参数:

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )

所有选项列表都可以在这里找到: http://www.php.net/manual/en/libxml.constants.php

这是抑制警告的正确方法:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING);

Using @ is just plain dirty.

If you look at the manual, there is an options parameter:

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )

All option list is available here: http://www.php.net/manual/en/libxml.constants.php

This is the correct way to suppress warnings:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING);
或十年 2024-08-21 19:22:10

你在谈论两件不同的事情。 HTTP 错误与 XML 文件是否有效无关,因此您需要考虑两个不同的错误处理区域。

您可以利用 libxml_use_internal_errors()抑制任何 XML 解析错误,然后手动检查它们(使用 libxml_get_errors()) 在每次解析操作之后。我建议这样做,因为您的脚本不会产生大量 E_WARNING 消息,但您仍然会找到无效的 XML 文件。

至于 HTTP 错误,处理这些错误将取决于您如何连接到 Web 服务并检索数据。

You're talking about two different things. HTTP errors will have nothing to do with whether an XML file is valid, so you're looking at two separate areas of error handling.

You can take advantage of libxml_use_internal_errors() to suppress any XML parsing errors, and then check for them manually (using libxml_get_errors()) after each parse operation. I'd suggest doing it this way, as your scripts won't produce a ton of E_WARNING messages, but you'll still find the invalid XML files.

As for HTTP errors, handling those will depend on how you're connecting to the webservice and retrieving the data.

烟火散人牵绊 2024-08-21 19:22:10

如果您对 Web 服务失败时的错误报告或日志记录不感兴趣,您可以使用错误抑制运算符:

$xml= @simplexml_load_file('http://tri.ad/test.xml');
if ($xml) {
 // Do some stuff . . .
}

但这只是一个简单的 hack。更强大的解决方案是使用 cURL 加载 XML 文件,记录任何失败的请求,解析任何 XML使用 simplexml_load_string 返回的文档,记录任何 XML 解析错误,然后对有效的 XML 执行一些操作。

If you're not interested in error reporting or logging when the webservice fails you can use the error supression operator:

$xml= @simplexml_load_file('http://tri.ad/test.xml');
if ($xml) {
 // Do some stuff . . .
}

But this is a simple hack. A more robust solution would be to load the XML file with cURL, log any failed requests, parse any XML document returned with simplexml_load_string, log any XML parse errors and then do some stuff with the valid XML.

远山浅 2024-08-21 19:22:10

出错时,您的 simplexml_load_file 应返回 false。

因此,做一些像这样简单的事情:

   $xml = @simplexml_load_file('myfile');
   if (!$xml) {
      echo "Uh oh's, we have an error!";
   }

是检测错误的一种方法。

On error, your simplexml_load_file should return false.

So doing somethign as simple as this:

   $xml = @simplexml_load_file('myfile');
   if (!$xml) {
      echo "Uh oh's, we have an error!";
   }

Is one way to detect errors.

心凉 2024-08-21 19:22:10

您可以在 PHP 中设置一个错误处理程序,以在出现任何 PHP 错误时抛出异常:(示例和更多文档可在此处找到:PHP.net)

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

You can set up an error handler within PHP to throw an Exception upon any PHP Errors: (Example and further documentation found here: PHP.net)

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
沙与沫 2024-08-21 19:22:10

另一种选择是使用 libxml_use_internal_errors() 函数来捕获错误。然后可以使用 libxml_get_errors() 函数检索错误。如果您想检查具体错误是什么,这将允许您循环遍历它们。如果您确实使用此方法,则需要确保在完成错误后将其从内存中清除,这样它们就不会浪费您的内存空间。

这是一个例子:

<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){
        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);

另一个例子实际上利用了我们捕获的错误:

<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){

        echo "Your script is not valid due to the following errors:\n";

        //Process error messages
        foreach(libxml_get_errors() as $error){
           echo "$error";
        }

        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);

Another option is to use the libxml_use_internal_errors() function to capture the errors. The errors can then be retrieved using the libxml_get_errors() function. This will allow you to loop through them if you want to check what the specific errors are. If you do use this method, you will want to make sure that you clear the errors from memory when you are done with them so they are not wasting your memory space.

Here is an example:

<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){
        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);

Another example actually making use of the errors we captured:

<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){

        echo "Your script is not valid due to the following errors:\n";

        //Process error messages
        foreach(libxml_get_errors() as $error){
           echo "$error";
        }

        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);
回眸一笑 2024-08-21 19:22:10
if (!$xml=simplexml_load_file('./samplexml.xml')) {  
    trigger_error('Error reading XML file',E_USER_ERROR);
}

foreach ($xml as $syn) {
    $candelete = $syn->candelete;
    $forpayroll = $syn->forpayroll;
    $name = $syn->name;
    $sql = "INSERT INTO vtiger (candelete, forpayroll, name) VALUES('$candelete','$forpayroll','$name')";
    $query = mysql_query($sql);
}
if (!$xml=simplexml_load_file('./samplexml.xml')) {  
    trigger_error('Error reading XML file',E_USER_ERROR);
}

foreach ($xml as $syn) {
    $candelete = $syn->candelete;
    $forpayroll = $syn->forpayroll;
    $name = $syn->name;
    $sql = "INSERT INTO vtiger (candelete, forpayroll, name) VALUES('$candelete','$forpayroll','$name')";
    $query = mysql_query($sql);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文