1

There was already a question similar to this, however it didn't quite answer the question.

So my question is, if I have a PhantomJS script which is executed via exec in PHP, could I also pass some arguments to it which the PhantomJS script could then somehow get and use? If so, how would I pass them from the PHP and how would I use them in the PhantomJS script? If not, is there any other way of doing this? Thanks.

1
  • Can you provide a link to the question you're referencing? Commented Mar 28, 2017 at 22:21

1 Answer 1

1

Run the following in PHP:

$response = exec('/path/to/phantomjs myscript.js arg1 arg2');

Then read the arguments from system.args (as documented in http://phantomjs.org/api/system/property/args.html)

var system = require('system');
var args = system.args;

args.forEach(function(arg, i) {
  console.log(i + ': ' + arg);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Great answer, but please note that this way of running exec will only put one last line of output in $response.
Any ideas why this PHP doesn't return anything? PHP: echo exec('~/usr/bin/phantomjs --version'); ? It is indeed there: gyazo.com/663a1b302dcfd894c21ff6f072853c52
Ah, stupid me haha, you don't need to specify root, /usr/bin/phantomjs worked. Thanks again man, really happy I got my phantomjs working.

Your Answer

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