DOMDocument loadHTML在服务器上无法正常工作

问题描述:

我首先在MAMP上运行代码,并且效果很好.但是,当我尝试在另一台服务器上运行代码时,我收到了很多警告,例如:

I run the code first on MAMP and it worked very well. But when I tried to run the code on another server, I got a lot of warnings like:

警告:DOMDocument :: loadHTML():意外的结束标记:实体中的标头,第17行的/cgihome/zhang1/html/cgi-bin/getPrice.php中的3349行警告:DOMDocument :: loadHTML():htmlParseStartTag:放错位置的标记实体行:/cgihome/zhang1/html/cgi-bin/getPrice.php中的3350行第17行警告:DOMDocument :: loadHTML():标记标头在中无效实体行:/cgihome/zhang1/html/cgi-bin/getPrice.php中的3517行第17行

Warning: DOMDocument::loadHTML(): Unexpected end tag : head in Entity, line: 3349 in /cgihome/zhang1/html/cgi-bin/getPrice.php on line 17 Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced tag in Entity, line: 3350 in /cgihome/zhang1/html/cgi-bin/getPrice.php on line 17 Warning: DOMDocument::loadHTML(): Tag header invalid in Entity, line: 3517 in /cgihome/zhang1/html/cgi-bin/getPrice.php on line 17

代码如下:

<?php
 $amazon = file_get_contents('http://www.amazon.com/blablabla');
 $doc = new DOMdocument();
 $doc->loadHTML($amazon);
 $doc->saveHTML();
 $price = $doc -> getElementById('actualPriceValue')->textContent;
 $ASIN = $doc -> getElementById('ASIN')->getAttribute('value');
?>

任何人都知道发生了什么事吗?谢谢!

Anyone knows what's going on? Thanks!

要禁用警告,可以使用

libxml_use_internal_errors(true);

这对我有用.手册

背景:您正在加载无效的HTML.无效的HTML很常见, DOMDocument :: loadHTML 可以纠正大多数问题,但默认情况下会发出警告.

Background: You are loading invalid HTML. Invalid HTML is quite common, DOMDocument::loadHTML corrects most of the problems, but gives warnings by default.

使用 libxml_use_internal_errors 您可以控制该行为.在加载文档之前进行设置:

With libxml_use_internal_errors you can control that behavior. Set it before loading the document:

libxml_use_internal_errors(true);
$doc->loadHTML($amazon);