0

This is a simple php code

<?php
ob_start(passthru('/usr/bin/env node getResult.js '));
$data = ob_get_contents();
ob_end_clean();
//  console shows Data as String but $data is still empty
    echo "Data : " . $data;
?>

And the node.js script just has a variable result with an object in it.

console.log("skript startet");
var get = function(){
/*do stuff to get variable*/
        result = "test;
        console.log(result);
        return result;
      });
    });
};
get();

Problem: I need the variable in the getResult.js script but I cant' catch it in php. Any Ideas ?

nova
  • 313
  • 6
  • 19

2 Answers2

1

Why bother with the output buffer at all?

exec( '/usr/bin/env node getResult.js', $data );
echo "Data: " . $data;

exec() writes the complete output of the command into the provided variable.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
0

Is this what you need?

<?php
ob_start(); //note, no parameter here
passthru('/usr/bin/env node getResult.js '); // this should be captured
$data = ob_get_contents();
ob_end_clean();

echo "Data : " . $data;
?>
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • $data is still empty but script gets executed – nova Mar 04 '16 at 10:57
  • you mean the getResult.js script is executed? What is the output of that js script? And what is the output of the php script? – Chris Lear Mar 04 '16 at 11:05
  • php script is all what you see here, node script makes an mongodb call gets some data , i wrote it into a var and now i need the content of this war in my php code. – nova Mar 04 '16 at 11:11
  • Sounds like the node script doesn't actually output anything. Can you run it from the command line? What output do you get? Is it actually emitting the value of the variable? – Chris Lear Mar 04 '16 at 11:18
  • ofc I checked this if i log the result variable in my script , I see that its filled with data – nova Mar 04 '16 at 11:21
  • Sure, but does the node script actually print that data to `stdout`? In other words, is there a line like `console.log(data);`? Sorry to ask questions that might seem daft, but you keep talking about a javascript variable as though php can access its value directly. Which it can't. – Chris Lear Mar 04 '16 at 11:27