Pass parameter from php to casperjs / phantomjs

Edit: I answered my question, see below.

ORIGINAL: I have phantomjs and casperjs installed on my web server, and they both work fine. script I plan to create a relise on user input from my site, which is then passed to the casperjs script. Having caught a little, I noticed that I was stuck in the most basic task of user input. How to pass a variable from php to casperjs?

Please note: only test scripts are listed below.

My PHP script

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

So, how do I pass $ user_input to hello.js. My goal is that the user can enter a URL, which is then cleared.

+4
1

.

, phantomjs casperjs http://phantomjs.org/api/system/property/args.html

script .

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

Source: https://habr.com/ru/post/1537004/


All Articles