PHP simplexml_load_file - catch file errors

风流意气都作罢 提交于 2019-12-10 15:39:17

问题


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.


回答1:


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);



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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");



回答6:


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);



回答7:


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


来源:https://stackoverflow.com/questions/1917876/php-simplexml-load-file-catch-file-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!