使用 html 注释解析 xml 时出现 Guzzle 错误

Guzzle error on parsing xml with html comment in it

我从 guzzle 收到这个错误。 xml 中有一条 html 评论,我认为它破坏了它。任何与此有关的经验,特别是与 guzzle 有关的经验,因为正是在这一点上,我试图在 xml 处获得响应,这个错误是通过例如$res->xml().单独 $res 或 $res->getBody() 似乎对我没有帮助。

"Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php:168 Stack trace: #0 /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php(168): SimpleXMLElement->__construct('Please use a mi...', 2048, false, '', false) #1 /home/mso/public_html/connector/get_dat.php(23): GuzzleHttp\Message\Response->xml() #2 {main} Next exception 'GuzzleHttp\Exception\XmlParseException' with message 'Unable to parse response body into XML: String could not be parsed as XML' in /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php:174 Stack trace: #0 /home/mso/public_html/connector/get_data.php(23): GuzzleHttp\Message\Response->xml() #1 {main} thrown in /home/mso/public_html/connector/vendor/guzzlehttp/guzzle/src/Message/Response.php on line 17

您可以尝试在加载之前检查您的 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 评论:

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