Kev*_*n M 4 php parameters exec phantomjs casperjs
编辑:我回答了我自己的问题,请参阅下面的编辑.
原文:我的网络服务器上安装了phantomjs和casperjs,它们都运行正常.我计划创建的脚本依赖于我网站上的用户输入,然后将其传递给casperjs脚本.在摆弄了一下之后,我注意到我被困在用户输入的基本任务上.如何将变量从php传递给casperjs?
请注意,以下只是测试脚本.
我的PHP脚本
$user_input = $_POST['user_input'];
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
exec('/usr/local/bin/casperjs hello.js 2>&1',$output);
print_r($output);
Run Code Online (Sandbox Code Playgroud)
hello.js
var user_input = "http://example.com/";
var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: false,
loadPlugins: false
}
});
casper.start(user_input, function() {
this.echo(this.getTitle());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
那么我如何将$ user_input传递给hello.js.我的目标是用户可以输入一个随后被抓取的网址.
我自己找到了答案.
似乎phantomjs和casperjs支持命令行参数http://phantomjs.org/api/system/property/args.html
我的脚本现在看起来像这样.
test.php的
$user_input = $_POST['user_input'];
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
exec('/usr/local/bin/casperjs hello.js $user_input 2>&1',$output);
print_r($output);
Run Code Online (Sandbox Code Playgroud)
hello.js
var system = require('system');
var args = system.args;
var address = args[4]; //In my case 4 was the argument for $user_input, yours might be different, depending on your server setup.
var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: false,
loadPlugins: false
}
});
casper.start(address, function() {
this.echo(this.getTitle());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)