Dom loadHTML在服务器上无法正常工作 [英] Dom loadHTML doesn't work properly on a server
问题描述
警告:DOMDocument :: loadHTML() :意外的结束标签:头实体,
行:3349在/cgihome/zhang1/html/cgi-bin/getPrice.php第17行
警告:DOMDocument :: loadHTML():htmlParseStartTag:放错标签在
实体,行:3350 in /cgihome/zhang1/html/cgi-bin/getPrice.php on
line 17警告:DOMDocument :: loadHTML():标签头在
中无效实体,行: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(值);
?>
任何人都知道发生了什么?谢谢!
要禁用警告,可以使用
libxml_use_internal_errors(真);
这适用于我。 手册
背景:正在加载无效的HTML。无效的HTML很常见, DOMDocument :: loadHTML
纠正大部分问题,但默认情况下会发出警告。
使用 libxml_use_internal_errors
可以控制该行为。在加载文档之前设置它:
libxml_use_internal_errors(true);
$ doc-> loadHTML($ amazon);
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:
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
The codes are following:
<?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!
To disable the warning, you can use
libxml_use_internal_errors(true);
This works for me. Manual
Background: You are loading invalid HTML. Invalid HTML is quite common, DOMDocument::loadHTML
corrects most of the problems, but gives warnings by default.
With libxml_use_internal_errors
you can control that behavior. Set it before loading the document:
libxml_use_internal_errors(true);
$doc->loadHTML($amazon);
这篇关于Dom loadHTML在服务器上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!