0

Is it possible to download the resulting HTML code after the JavaScript code on the page has been run using PHP.

For example, when the page has this jQuery code $("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.

3 Answers 3

0

use 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

Sign up to request clarification or add additional context in comments.

Comments

0

Please refer to similar questions and share your research you have done so far:

Comments

0

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.

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.

Here's an example.

Javascript

<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.

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. If you're using this HTML to output on your website, it can easily be spoofed to reveal sensitive data on your website.

Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.