I am getting some products via some xml files daily.
Sometimes there are 4 files, sometimes there are up to 10.
I want to process them in a loop with SimpleXMLElement from an URL.
Here is what I try:
for ($i = 1; $i <= 10; $i++) {
try {
$SimpleXML = new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
} catch (Exception $e) {
var_dump($e);
}
}
But unluckily I've got warning:
SimpleXMLElement::__construct(): http://example.com/file_6.xml:42: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xE1 0x6D 0x6F 0x67
In this case, there were only 5 files, so when I try to get the 6th, that gaves me the main page from the site. I've also tried with to surpress the warning without success.
$SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
Is there any way to handle these errors in order to avoid them stopping my script?
EDIT
I can not use file_get_contents
because in this case, I am getting memory limit error, and I can not increase the memory. These are so big files, one of it is 1GB. Guess what, the developer of these files are put the product images binaries into the file :((((((((( (I can not speak with him).
EDIT2
In php documentation (here) I've read $var = @new some_class();
should work.
If I am using:
$SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
I get this:
Exception: String could not be parsed as XML in ........\Parser.php on line 19
I thought, yeah, it is much more better, because this is an Expection
. If I wrap with try/catch
, I just get the same error.
try {
$SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
} catch (Exception $e) {
die('Exception');
}
\MFF_System\SimpleXMLExtended
is just extends the SimpleXMLElement
class, and use a method to add CDATA
, nothing special.