0

I'm using curl to get a remote page.. I have a local js file.. Its called main.js.

I want to execute that js file on that curl page.

My main.js file outputs the result as JSON string.

I would like to assign that string in a php variable. Can someone help me?

2 Answers 2

4

To execute JavaScript you need a JavaScript engine. You can do this in a headless browser like phantomjs,or you can try with v8 js extension for PHP

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

Comments

0

Try

1) In the main.js

`myobj = new Array();
 myobj["field1"] = field1; 
 myobj["field2"] = $("#field2").val();
 myobj["field3"] = $("#field3").val();
 myobj["field4"] = field4; `

field1 and field 4 if you have the value directly and field2 and field 3 if it comes from a html form. Now put it

`FormArray(myobj);
 var data = JSON.stringify($("#form").serializeObject());` 

Now you need to call the php function that is going to receive the data. Ye it can be confused so it is a great method.

`$.ajax({
        url:    'yourphpfile.php?,
        data:   {
            module: 'path',
            datos:  data, // Your data flying to php here
        },
        async:      false,
        type:       'POST',
        success:    function(html){

        }`

Now in yourphpfile.php that is going to receive the data put

 `$datos = json_decode($datos);
  $field1 = $datos->field1;
  $field2 = $datos->field2
  $field3 = $datos->field3
  $field4 = $datos->field4`

Hope it help you!!

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.