LoginSignup
0
1

More than 5 years have passed since last update.

今更casperJSで遊んでみた〜phpから実行して引数を受け渡す〜

Last updated at Posted at 2017-03-27

はじめに

今更ですが、casperJSを使って色々試していたのですが
案外一連の流れでまとまっている記事がなかったのでメモとして残しておきます。
今回はPHPから引数を指定してjsを実行させて、その際渡した引数をjs側で受け取る方法。

 書き方

sample.php
$isSuccess = false;

$targetDate = '2017-03-27';
exec('casperjs sample.js ' . $targetDate, $resultData, $result);

// execの返値は正常の場合のみ0が返る
if ($result === 0) $isSuccess = true;
sample.js
var casper = require("casper").create();
var param = casper.cli.args[0];
console.log(param); // 2017-03-27 

ポイント

  • execを使ってjsの実行結果を受け取るようにするとエラー判定も可能
  • casper.cli.argsは配列の為、引数が複数ある場合はcasper.cli.args[1]等指定してあげるとよい。

関連記事

今更casperJSで遊んでみた〜cookieログイン編〜

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1