【问题标题】:Guzzle error on parsing xml with html comment in it [closed]解析带有html注释的xml时出现Guzzle错误[关闭]
【发布时间】:2015-03-17 08:56:57
【问题描述】:

我从 guzzle 那里得到这个错误。 xml 中有一个 html 注释,我认为这会破坏它。任何与 guzzle 相关的经验,因为正是在这一点上,我尝试在 xml 中获取响应,该错误是通过例如$res->xml()。单独的 $res 或 $res->getBody() 似乎对我没有帮助。

“未捕获的异常 'Exception' 带有消息 'String 不能被 解析为 XML' /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php:168 堆栈跟踪:#0 /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php(168): SimpleXMLElement->__construct('请使用 mi...', 2048, false, '', 错误)#1 /home/mso/public_html/connector/get_dat.php(23): GuzzleHttp\Message\Response->xml() #2 {main} 下一个异常 带有消息“无法解析”的“GuzzleHttp\Exception\XmlParseException” 将响应正文转换为 XML:无法将字符串解析为 XML' /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php:174 堆栈跟踪:#0 /home/mso/public_html/connector/get_data.php(23): GuzzleHttp\Message\Response->xml() #1 {main} 抛出 /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php 第 17 行

【问题讨论】:

标签: php xml guzzle


【解决方案1】:

您可以尝试在加载之前检查您的 xml:

$oXmlUtilities = new cXmlUtilities();
// Validate xml file by setting internal errors.
libxml_use_internal_errors( true );
try
{
    $sTmpString = simplexml_load_string( $sContents );
}
catch( Exception $oException )
{
    $sError = $oException->GetMessage();
}

// If xml validates and isn't empty call guzzle:
if( empty( $sError ) && ( $sTmpString ) )
{
    // XML is valid.

否则您可能需要通过 html cmets 的正则表达式来解析字符串:

<!--(.*?)-->

【讨论】:

  • 原来那个特定的 xml 提要中包含 guzzle 不喜欢的 cmets。当我切换到使用 curl 时它起作用了(虽然 guzzle 不也使用 curl)?。
猜你喜欢
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多