简体   繁体   English

使用file_get_content下载javascript输出

[英]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 希望能有所帮助

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.

相关问题 如何使用jquery / javascript获取任何网站的html源代码,例如php的file_get_content? - How can I get a html source code of any website using jquery/javascript like php's file_get_content? 调用SMS API的Ajax中没有php curl(GET / POST)/ file_get_content / wget响应 - no php curl(GET/POST)/file_get_content/wget response in ajax calling an SMS API 从php的file_get_content()函数访问控制台日志 - Access console logs from php's file_get_content() function 如果文件内容可用但没有文件直接链接,请使用Javascript下载文件 - File download using Javascript, if file content is available but no file direct link 使用javascript下载pdf文件内容 - Download pdf file content with javascript 使用Javascript / Angularjs下载文件,其内容以JSON格式检索 - File Download using Javascript / Angularjs, whose content is retrieved in JSON format 如何使用 javascript 将一些文本内容下载为 .ftl 文件? - How to download some text content as a .ftl file using javascript? 使用 JavaScript 下载文件 - Download a File using JavaScript 使用javascript获取文本文件内容 - Get text file content using javascript 使用JavaScript获取“文件”对象的内容类型 - Get content type of 'File' object using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM