3

I'm trying to parse a GuzzleHTTP\PSR7\Response into a SimpleXMLElement Object using simplexml_load_string() function. What I'm facing here is I get Randomly Exception that says :

Entity: line 1: parser error : Start tag expected, '<' not found

and by randomly I mean once I pass successfully, once fail.

What is your suggestion for simplexml_load_string() input/ How to fix the Exception?

PHP :

$xml = simplexml_load_string((string)$response->getBody());
Ramin Omrani
  • 3,673
  • 8
  • 34
  • 60

1 Answers1

2

The function simplexml_load_string requires a string as it's first parameter that contains well-formed XML. If you do not provide such a string, don't expect the function to work.

In that case, handle the error case and you're fine: The function will return false if the string couldn't be parsed as XML. The PHP-manual has a whole page about that topic alone:

We should also have Q&A material here on site, for example:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836