[英]Download javascript output using file_get_content
Is it possible to download the resulting HTML code after the JavaScript code on the page has been run using PHP. 使用PHP运行页面上的JavaScript代码后,是否可以下载结果HTML代码。
For example, when the page has this jQuery code $("p").html("Hello world");
例如,当页面具有此jQuery代码
$("p").html("Hello world");
and I use file_get_content('website.com')
I don't get the string "Hello world" because the JavaScript runs after the page load. 并且我使用
file_get_content('website.com')
,但由于页面加载后JavaScript会运行,因此无法获取字符串“ Hello world”。
use cURL: 使用cURL:
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Then do : 然后做 :
<?php echo get_data('http://theURLhere.com'); ?>
Hope that helped 希望能有所帮助
Please refer to similar questions and share your research you have done so far: 请参考类似的问题,并分享您到目前为止所做的研究:
Get the content (text) of an URL after Javascript has run with PHP 使用PHP运行Javascript后获取URL的内容(文本)
Get Document source After AJAX or js Action With PHP 使用PHP进行AJAX或js操作后获取文档源
How to get content of a javascript/ajax -loaded div on a site? 如何在网站上获取javascript / ajax加载的div的内容?
Get HTML code after javascript execution using CURL PHP 使用CURL PHP在javascript执行后获取HTML代码
One way to achieve this would be to use Selenium, and write a custom script to gather the output from it... But I'm sure that falls far beyond the scope of what you're attempting to do. 实现这一目标的一种方法是使用Selenium,并编写一个自定义脚本以从中获取输出...但是我敢肯定,这远远超出了您尝试做的事情的范围。
The way I would go would be to invert the responsibility. 我要走的路是倒转责任。 Have the JS send the output to a PHP endpoint, and use that output however you see fit.
让JS将输出发送到PHP端点,并使用该输出,但您认为合适。
Here's an example. 这是一个例子。
Javascript Java脚本
<script>
var outputElement = 'html';
var HTML = $(outputElement).html();
var endpoint = 'myEndpoint.php';
$.post(endpoint, { html: HTML }, function(data) {
alert('Output sent');
});
</script>
One caveat here is that you will not get the DOCTYPE declaration, or any attributes on your HTML tag, if this isn't acceptable, you may reconstruct them in the PHP file below. 需要注意的是,您将无法获得DOCTYPE声明或HTML标记上的任何属性,如果不能接受,则可以在下面的PHP文件中重构它们。
PHP 的PHP
<?php
$html = $_POST['html']; // Be VERY CAREFUL with what you do with this...
// If you need to have the doctype and html tag... Use your own doctype.
// $html = sprintf('<DOCTYPE html><html class="my-class">%s</html>', $html);
// Do something with the HTML.
You have to be very careful when sending HTML over POST. 通过POST发送HTML时必须非常小心 。 If you're using this HTML to output on your website, it can easily be spoofed to reveal sensitive data on your website.
如果您使用此HTML在您的网站上输出,则很容易被欺骗以泄露网站上的敏感数据。
Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.