编辑:我回答了我自己的问题,请参阅下面的编辑。
原创: 我的网络服务器上安装了 phantomjs 和 casperjs,它们都运行良好。我计划创建的脚本依赖于来 self 网站的用户输入,然后将其传递给 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);
你好
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();
那么我如何将 $user_input 传递给 hello.js。我的目标是用户可以输入一个 url 然后被抓取。
最佳答案
我自己找到了答案。
似乎 phantomjs 和 casperjs 支持命令行参数 http://phantomjs.org/api/system/property/args.html
我的脚本现在看起来像这样。
测试.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);
你好
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();
关于php - 将参数从 php 传递到 casperjs/phantomjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141628/