Pass parameter from php to casperjs/phantomjs

前端 未结 1 1333
说谎
说谎 2021-01-03 05:28

Edit: I answered my own question, see edit below.

ORIGINAL: I have phantomjs and casperjs installed on my web server and they both run fine. The script I am planning

1条回答
  •  有刺的猬
    2021-01-03 06:19

    I found the answer myself.

    It seems phantomjs and casperjs support command line arguments http://phantomjs.org/api/system/property/args.html

    My script now looks like this.

    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,
      logLevel: 'error',
      pageSettings: {
        loadImages: false,
        loadPlugins: false
      }
    });
    
    casper.start(address, function() {
        this.echo(this.getTitle());
    });
    
    casper.run();
    

    0 讨论(0)
提交回复
热议问题