加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

将参数从php传递给casperjs / phantomjs

发布时间:2020-12-13 17:30:23 所属栏目:PHP教程 来源:网络整理
导读:编辑:我回答了我自己的问题,请参阅下面的编辑. 原版的: 我在我的网络服务器上安装了phantomjs和casperjs,它们都运行正常.我计划创建的脚本依赖于我网站上的用户输入,然后将其传递给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);

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();

那么我如何将$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);

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,loadPlugins: false
  }
});

casper.start(address,function() {
    this.echo(this.getTitle());
});

casper.run();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读