0

Lets say I have a public html page on the internet with the following content:

<html>
    <head>
        <script>
            var variableA = {
                name: "A"
            };
            variableA.id = 1;
        </script>
    </head>
    <body>
        ...
    </body>
</html>

Is there a way to get the value of the variable variableA with php?

Something like this:

$html = file_get_contents($myUrl);
$variableA = magicMethodIamLookingFor("variableA");
print_r($variableA);

With this result:

Array(
    "name" => "A",
    "id" => 1
)

I could do it just with regex and json_decode (similar like How to get Javascript variable from an HTML page?) but the problem is, that its not just a single json, but there are also variable changes like variableA.id = 1;

bernhardh
  • 3,137
  • 10
  • 42
  • 77
  • 1
    You can use regex to finding it – Mohammad Nov 07 '18 at 11:31
  • 1
    Possible duplicate of [How to get Javascript variable from an HTML page?](https://stackoverflow.com/questions/37883428/how-to-get-javascript-variable-from-an-html-page) – Mohammad Nov 07 '18 at 11:35
  • You can for example send the variable value in a form or simply create an HTTP request and send it as URL parameter. In both cases, the target URL will be your PHP script. – David Ferenczy Rogožan Nov 07 '18 at 11:45
  • @DawidFerenczy: I have no access on the public url. @Mohammad: That would be possible, but since the variable is change (`variableA.id = 1;`) it will be really tricky to do that that way – bernhardh Nov 07 '18 at 14:31
  • I'm not getting it. What's on the "public url"? Do you mean that you can't modify the HTML page by that? – David Ferenczy Rogožan Nov 07 '18 at 15:08
  • Its not my domain. I can't modify anything on it. I am writing a crawler to crawl a couple of webpages for a specialized search engine. And on it, there is some data stored in JS variables – bernhardh Nov 07 '18 at 15:35

1 Answers1

0

Note: With plain PHP scripting, it is not possible to access JS vars.

Interesting requirement. Below goes a solution. Lets consider the case if the variable name is passed in the URL. Let the page name be 'index.php'. The browsing URL looks like this localhost/index.php?varName=variableA, where varName is the query param which takes variable name as value. We can pass comma separated values also by tweaking the code a bit. But that is not considered now.

Outlining the steps below

  1. Get contents of url.php and place it in a hidden div of index.php
  2. Beneath the captured content, call an Ajax function with var name & var value as param.
  3. Inside the ajax page, save the name / value to some DB or file.
  4. After the ajax call there is some more DOM where we will print the name / value & remove the record from DB or File after displaying the same. NB: The main code is in JS - See getVarWithValue(variable) function.

    <html>
    <body>
    
        <?php 
            $varName = (isset($_GET['varName']) && trim($_GET['varName']) != "") ? $_GET['varName'] : 'variableA';
        ?>
    
        <div style="display:none">
            <?php
                $html = file_get_contents('./url.php');
                echo $html;
    
            ?>
            <script>
                //THIS IS THE CORE FUNCTION WHICH GETS THE VAR NAME & VALUE
                function getVarWithValue(variable) {
                    var param = '';
                    for (var name in this) {
                        keyValue = [];
                        if (variable == name ) return param += name+'='+JSON.stringify(eval(name));
                    }
                    return false;
                }
    
                var http = new XMLHttpRequest();
                http.open("POST", "setVariableValue.php", false); // 3rd argument makes sure that the call is NOT async.  
                http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                http.onreadystatechange = function() {
                    if (http.readyState == 4 && http.status == 200) {
                        document.getElementById("varValue").innerHTML = http.responseText.toString();
                    }
                };
                http.send(getVarWithValue( '<?php echo $varName ?>' ));
            </script>
        </div>
        <?php
            //$varValue = getValuefromDB(); Get the saved value from DB or file
            echo "The variable value of $varName is: ".$varValue;
            // Write code to remove the row from DB or file
        ?>
    </body>
    

url.php (Page from which we need to capture the variable name & value)

<html>
    <head>
        <script>
            var variableA = {  name: "A" };
            variableA.id = 1;
            var variableB = ["Stack", "Overflow"]
        </script>
    </head>
    <body>
        Some Text
    </body>
</html>

setVariableValue.php

<?php
/**
* Write code for saving var name & value to DB or file
*/