0

I want to GET a XML feed using simplexml_load_file() and I want to handle the errors by myself. The problem is, libxml_use_internal_errors(true) does not remove all the warnings. Here is my exact code:

<?php
libxml_use_internal_errors(true);
$url = "http://feeds.feedburner.com/GoogleOnlineSecurityBlog";
$res = simplexml_load_file($url);

When I purposefully write a typo in the URL, I get these warnings:

Warning: simplexml_load_file(): php_network_getaddresses: getaddrinfo failed: Unknown host. in C:\xampp\htdocs\test.php on line 4

Warning: simplexml_load_file(http://feeds.feedburener.com/GoogleOnlineSecurityBlog): failed to open stream: php_network_getaddresses: getaddrinfo failed: Unknown host. in C:\xampp\htdocs\test.php on line 4

Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://feeds.feedburener.com/GoogleOnlineSecurityBlog" in C:\xampp\htdocs\test.php on line 4 error

Adding or removing libxml_use_internal_errors(true) only adds/removes the last warning and not the first 2. What did I do wrong? I have PHP 7.2.2.

Thank you for your help.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
  • 1
    These are completely different _types_ of error. `libxml_use_internal_errors` says how errors should be handled that occur when trying to parse the XML. You don’t even get that far here, because this fails well before any XML that could be parsed is even in play - your network request to try and fetch the data in the first place failed here, and that has nothing whatsoever to do with the _internals_ of the XML parser library. – 04FS Oct 22 '19 at 11:50
  • Ok thanks, but do you know how to intercept these errors? – JacopoStanchi Oct 22 '19 at 11:56
  • 1
    Same as with any “normal” errors / warnings / notices … `@` before the function call (_not_ the recommended option, go research why), or by simply configuring PHP to just _logs_ errors, instead of showing them as part of the output, which is the general way to do this in a production system. And to determine whether your script should even proceed after, you of course check the return value of the function call, as usual. – 04FS Oct 22 '19 at 11:59
  • Possible duplicate of [php hide ALL errors](https://stackoverflow.com/questions/9242903/php-hide-all-errors) – miken32 Oct 22 '19 at 19:43

0 Answers0