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
simplexml_load_string - Exception => Entity: line 1: parser error : Start tag expected, '<' not found
提问by Ramin
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 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_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.
该函数simplexml_load_string
需要一个字符串,因为它是包含格式良好的 XML的第一个参数。如果您不提供这样的字符串,请不要指望该函数可以工作。
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:
在这种情况下,处理错误情况就可以了:false
如果无法将字符串解析为 XML,该函数将返回。PHP 手册有一整页关于该主题的内容:
We should also have Q&A material here on site, for example:
我们还应该在现场提供问答材料,例如:
- simplexml error handling php(Aug 2009)
- How to shut up simpleXML on malformed data?(Nov 2009)
- simplexml 错误处理 php(2009 年 8 月)
- 如何在格式错误的数据上关闭 simpleXML?(2009 年 11 月)