0

我正在写这个答案,因为我是 DomDocuement 的新手,我无法在其他任何地方找到答案。

我正在为 CMS 网站编写自定义 WordPress 主题。一项要求是从其他站点检索网页内容,选择要显示的部分内容,然后参考原始站点以获取完整内容。

因此,我使用wp_remote_getWordPress 的功能来获取网页。然后我必须解析它以转到特定的网页元素,我正在使用 Domdocument 解析页面如下

$dom = new domDocument();
$dom->loadHTML($html); // Where $html is the page retrieved earlier using wp_remote_get

现在,问题是 loadHTML() 导致了这些警告:

PHP Warning:  DOMDocument::loadHTML(): DOCTYPE improperly terminated in Entity, Lline: 2
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <html> tag in Entity, line: 3
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <head> tag in Entity, line: 4
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity, line: 105
PHP Warning:  DOMDocument::loadHTML(): ID 1 already defined in Entity, line: 551

还有更多警告。

现在显然这个网页有问题,但是我们需要检索的大多数页面都会产生这些错误。

我的问题是:

  1. 我应该担心这些警告吗
  2. 有没有办法告诉 domdocuemnt 忽略这些缺陷?
  3. 我应该怎么做才能使事情正常进行

感谢您的输入

4

0 回答 0