From ab13f8f82091d5c54206a3223e68178340a9bfc5 Mon Sep 17 00:00:00 2001 From: Zorn Date: Fri, 8 Sep 2023 16:41:09 -0600 Subject: [PATCH] feat: handle broken XML documents --- src/Feed.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Feed.php b/src/Feed.php index 3a59c27..6a46488 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -193,7 +193,14 @@ private static function loadXml($url, $user, $pass) throw new FeedException('Cannot load feed.'); } - return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); + $doc = new DOMDocument(); + $doc->loadXML($data, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); + + if ($doc->documentElement !== NULL) { + return new SimpleXMLElement($doc->saveXML(), LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); + } else { + return new SimpleXMLElement('', LIBXML_NOWARNING | LIBXML_NOERROR); + } }