PHP使用DOMDocument从URL检索内部HTML作为字符串

Dan*_*nze 1 html php dom cross-domain

我一直在挑选一些零碎的代码,你可以大致看到我想要做什么,显然这不起作用并且完全错误:

<?php

$dom= new DOMDocument();
$dom->loadHTMLFile('http://example.com/');
$data = $dom->getElementById("profile_section_container");
$html = $data->saveHTML();
echo $html;

?>
Run Code Online (Sandbox Code Playgroud)

使用 CURL 调用,我能够检索文档 URL 源:

function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);

if ($contents) return $contents;
else return FALSE;
}

$f = curl_get_file_contents('http://example.com/'); 
echo $f;
Run Code Online (Sandbox Code Playgroud)

那么我现在如何使用它在 PHP 中实例化一个 DOMDocument 对象并使用getElementById提取一个节点

anu*_*ava 5

这是您需要避免任何格式错误的 HTML 错误的代码:

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://example.com/');
$data = $dom->getElementById("banner");
echo $data->nodeValue."\n"
Run Code Online (Sandbox Code Playgroud)

要转储整个 HTML 源代码,您可以调用:

echo $dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)