php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54950 simplexml_load_[string|file]: error_get_last is useless
Submitted: 2011-05-29 18:07 UTC Modified: 2018-08-07 17:32 UTC
From: marc-bennewitz at arcor dot de Assigned:
Status: Verified Package: SimpleXML related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-05-29 18:07 UTC] marc-bennewitz at arcor dot de
Description:
------------
On loading an invalid xml catched errors using error_get_last aren't helpful because the message is nearly empty "simplexml_load_string():         ^"

It looks like there are more errors reported and the last error is the most unhelpful error but this is the only good catchable error (see example).

A workaround only exists using an own error handler but it's nasty :(

Test script:
---------------
$xml = <<<XML
<?xml version="1.0"?>
<config>
    <a>test</a>
</other>
XML;

echo 'LOAD INVALID XML STRING' . PHP_EOL;
$simpleXML = simplexml_load_string($xml);

echo 'DETECTED ERROR:' . PHP_EOL;
if ($simpleXML === false) {
    $err = error_get_last();
    var_dump($err);
}

Expected result:
----------------
LOAD INVALID XML STRING
PHP Warning:  simplexml_load_string(): Entity: line 4: parser error : Opening and ending tag mismatch: config line 2 and other in /tmp/bug_simpleXmlLoad.php on line 11

Warning: simplexml_load_string(): Entity: line 4: parser error : Opening and ending tag mismatch: config line 2 and other in /tmp/bug_simpleXmlLoad.php on line 11
DETECTED ERROR:
array(4) {
  ["type"]=>
  int(2)
  ["message"]=>
  string(34) "simplexml_load_string(): Entity: line 4: parser error : Opening and ending tag mismatch: config line 2 and other"
  ["file"]=>
  string(26) "/tmp/bug_simpleXmlLoad.php"
  ["line"]=>
  int(11)
}

Actual result:
--------------
LOAD INVALID XML STRING
PHP Warning:  simplexml_load_string(): Entity: line 4: parser error : Opening and ending tag mismatch: config line 2 and other in /tmp/bug_simpleXmlLoad.php on line 11

Warning: simplexml_load_string(): Entity: line 4: parser error : Opening and ending tag mismatch: config line 2 and other in /tmp/bug_simpleXmlLoad.php on line 11
PHP Warning:  simplexml_load_string(): </other> in /tmp/bug_simpleXmlLoad.php on line 11

Warning: simplexml_load_string(): </other> in /tmp/bug_simpleXmlLoad.php on line 11
PHP Warning:  simplexml_load_string():         ^ in /tmp/bug_simpleXmlLoad.php on line 11

Warning: simplexml_load_string():         ^ in /tmp/bug_simpleXmlLoad.php on line 11
DETECTED ERROR:
array(4) {
  ["type"]=>
  int(2)
  ["message"]=>
  string(34) "simplexml_load_string():         ^"
  ["file"]=>
  string(26) "/tmp/bug_simpleXmlLoad.php"
  ["line"]=>
  int(11)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-29 19:37 UTC] marc-bennewitz at arcor dot de
OK 'libxml_use_internal_errors' can also be used
but the last reported error is unhelpful anyhow
 [2018-08-07 17:32 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2018-08-07 17:32 UTC] cmb@php.net
I can confirm the reported behavior: <https://3v4l.org/69E3Y>.
DOM handles this better (<https://3v4l.org/4fIVO>).

> OK 'libxml_use_internal_errors' can also be used
> but the last reported error is unhelpful anyhow

I can't confirm this: <https://3v4l.org/3eFuK>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC