laravel simplexml_load_string - 异常 => 实体:第 1 行:解析器错误:预期开始标记,未找到“<”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31876761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:08:49  来源:igfitidea点击:

simplexml_load_string - Exception => Entity: line 1: parser error : Start tag expected, '<' not found

phpxmllaravelsimplexmlguzzle

提问by Ramin

I'm trying to parse a GuzzleHTTP\PSR7\Responseinto a SimpleXMLElementObject using simplexml_load_string()function. What I'm facing here is I get RandomlyException that says :

我正在尝试使用函数GuzzleHTTP\PSR7\Response将 a解析为一个SimpleXMLElement对象simplexml_load_string()。我在这里面临的是我得到随机异常说:

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

实体:第 1 行:解析器错误:需要开始标记,未找到“<”

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?

您对simplexml_load_string()输入/如何修复异常有什么建议?

PHP :

PHP :

$xml = simplexml_load_string((string)$response->getBody());

回答by hakre

The function simplexml_load_stringrequires 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.

该函数simplexml_load_string需要一个字符串,因为它是包含格式良好的 XML的第一个参数。如果您不提供这样的字符串,请不要指望该函数可以工作。

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

在这种情况下,处理错误情况就可以了:false如果无法将字符串解析为 XML,该函数将返回。PHP 手册有一整页关于该主题的内容:

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

我们还应该在现场提供问答材料,例如: