Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP simplexml_load_file - catch file errors

Tags:

php

xml

simplexml

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.

like image 697
yoda Avatar asked Dec 16 '09 21:12

yoda


1 Answers

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);
like image 172
Mārtiņš Briedis Avatar answered Sep 27 '22 22:09

Mārtiņš Briedis