假设有一个HTML页面,其中包含以下代码 -
<body>
<div id='some_id'></div>
<script>
var div = document.getElementById("some_id");
var input = document.createElement("input");
input.type="text";
input.value="text box";
div.appendChild(input);
</script>
</body>
现在,如果我做查看源代码,我将看到上面的代码。但是如果我使用萤火虫,我会看到这个——
<body>
<div id='some_id'><input type="text"></div>
<script>
var div = document.getElementById("some_id");
var input = document.createElement("input");
input.type="text";
input.value="text box";
div.appendChild(input);
</script>
</body>
也就是说,最终呈现的 HTML 输出是不同的。假设此文件位于另一台服务器上,例如 - http://www.someanotherserver.com/some_file.html
现在是否可以在 PHP 中获取最终渲染的 HTML 输出? file_get_contents
我想只会显示我在视图源中看到的内容。
使用 PHP,没有办法获得最终渲染的 HTML。这是因为对原始源代码所做的更改是在 javascript 执行时完成的。这需要你有一个Javascript引擎/运行时(你的浏览器有一个)。
执行file_get_contents()
与执行curl
或wget
相同 - 它查询服务器,但不执行任何JS。