Jim Dee
Jim Dee

Reputation: 51

PHP / cURL or file_get_contents -- How to get the value of a JS page instead of the JS code

This is hopefully an easy question, but it's giving me an issue. I have a page "sample.html" with a simple script like this in JS:

// sample.html
<script>
    let myNumber = 15;
    document.write(myNumber);
</script>

... and then I want to have a PHP page ("getValue.php") go and get that number (e.g., 15). My problem is: If I do a file_get_contents or even a cURL to that JS page, like so...

// getValue.php
$newVal = file_get_contents('sample.html');

... the $newVal var in PHP is NOT simply the "15" value. Instead, the var is equal to the entire JS script shown. So, how can I use PHP to get that "15" value alone as a variable? In other words, I only want $newVar to be the result of the JS page, not the entire script. What I want to achieve is:

// $newVal == 15

Upvotes: 3

Views: 161

Answers (1)

Maik Lowrey
Maik Lowrey

Reputation: 17556

TL;DR

With file_get_content you cant do that.

You looking for a php scrapper of JavaScript genertaed content. One tool would be PHP-Webdriver. It base on Selenium.

https://github.com/php-webdriver/php-webdriver

Upvotes: 3

Related Questions