I'm trying to run a simple phantomjs program passing some args from php and the only arg that my js can read is the first one (args[0]).
Here is the code.
PHP:
...
$resp = exec("phantomjs phantom/test.js hello other args");
...
phantom/test.js:
var args = require('system').args;
str = 'Args: ';
args.forEach(function (arg, i) {
str += (i + ': ' + arg);
});
str += ' Length: '+args.length;
str = str.replace(/(
|
|)/gm,"");
console.log(str);
phantom.exit();
The output is:
Args: 0: phantom/test.js Length: 1
I'm using Windows 7. I tried to run this code direcly from the windows command line (cmd-> 'phantomjs test.js some args') and it worked properly, so, I think that the problem lies on php and its relation with the "require('system')" of phantomjs.
Srry for my horrible english.
Thanks a lot !
What you are doing is appending string into a single variable.
Try the code below
$resp = exec("phantomjs phantom/test.js arg1 arg2 ..... argN");
Now the phantomjs code would be like this to catch the arguments sent from Php file
var system = require('system');
var args = system.args;
var argument1 = args[1];
var argument2 = args[2];
.
.
.
var argument3 = args[N];
You can use values stored the variables.