【问题标题】:Pass parameter from php to casperjs/phantomjs将参数从 php 传递给 casperjs/phantomjs
【发布时间】:2014-04-17 19:18:04
【问题描述】:

编辑:我回答了我自己的问题,请参阅下面的编辑。

原件: 我的网络服务器上安装了 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);

你好.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();

那么我如何将 $user_input 传递给 hello.js。我的目标是用户可以输入一个 url,然后被抓取。

【问题讨论】:

  • 您可以将该答案发布为“答案”并接受它。这是正确的做法。
  • 是的,请将其发布为自我回答。您的答案基本上是正确的,但请使用escapeshellarg(即使您知道这一点,将来也可能不会有人从您的答案中复制和粘贴)。 (例如 user_input = ';rm -rf /etc/' )
  • 我不得不等待 48 小时才能回答自己。我现在修好了。谢谢

标签: php parameters exec phantomjs casperjs


【解决方案1】:

我自己找到了答案。

好像 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);

你好.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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多