用PHP中的PhantomJS截屏,并将参数传递给脚本


Take screenshot with PhantomJS from PHP and pass arguments to the script

我需要传递变量,但不能工作

php

exec('phantomjs screenshot.js http://www.google.com google.png');

js

var args = require('system').args;
var page = require('webpage').create();
var address = system.args[1];
var image = system.args[2];
page.open(address , function () {
    page.render(image);
    phantom.exit();
});

如果我在没有php的情况下运行脚本,我就有

    phantomjs screenshot.js http://google.com google.com
0: screenshot.js
1: http://google.com
2: google.com
ReferenceError: Can't find variable: system

Phantomjs找不到system变量,因为它未定义。

你需要写任一

var args = require('system').args;
var address = args[1];
var image = args[2];

或者你可以写

var system = require('system');
var address = system.args[1];
var image = system.args[2];