使用PHP获取加载的操作HTML dom

使用PHP获取加载的操作HTML dom

问题描述:

I tried to get a loaded and with Javascript manipulated HTML dom, with PHP. But it only returns the documents content.

For me it seems to be, that theres no solution :(

Example:

index.php
$dom = new DOMDocument();
$dom->load('test.html');
$html = $dom->getElementsByTagName("html")->item(0);
echo $html->nodeValue;

Result:

title
var test = document.createElement("div");
test.innerHTML = "test";
document.body.appendChild(test);

I try all to get this result:

<head>
<title>title</title>
</head>
<body>
<div>test</div>
</body>

You can't. At least, not with PHP alone. The PHP DOM extension does not include a Javascript interpreter; it will only report on the literal contents of the HTML file.

It might be possible to load the document into a headless browser, like PhantomJS. But this will be a lot more work, and isn't possible without using a significant amount of software outside the PHP standard library.