dongqie8661
dongqie8661
2014-02-04 16:04

Centos服务器上的CasperJs如何使用php将变量传递给casper js

  • phantomjs
  • php
  • casperjs
已采纳

Okay so i have a casperjs script which does a specific task for me then echoes a result back to me.

i normally use the command

Casperjs script.js "variable1" "variable2"

after a few moments it will echo a results saying either

True

or

False

how can i use php on my server instead of using the command line i would like the php script to execute the command above and then echo the result back to me on my php page

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxu9707 douxu9707 7年前

    You can use PHP's shell_exec which uses php to call your command, like so:

    .php

    $casperjs = "casperjs";
    $script = "script.js";
    $arg0 = $variable1; 
    $arg1 = $variable2;
    $command = "$casperjs $script $arg0 $arg1";
    $result = shell_exec($command);
    echo $result;   
    

    script.js

    var casper = require('casper').create();
    var arg0 = casper.cli.get(0);
    var arg1 = casper.cli.get(1);
    casper.start('http://example.com/', function() {
      if (this.getTitle() == 'blahblah') {
        this.echo('True');
      } else {
        this.echo('False');
      }
    });     
    casper.run();
    
    点赞 评论 复制链接分享