2

I have a casperJS script which returns JSON when run via commandline. I want to make an API preferably in PHP which runs the script as in commandline (casperJS sample.js) and returns the JSON as result.

user2129794
  • 2,388
  • 8
  • 33
  • 51

2 Answers2

3

I think you can use php exec() function as shown below to do this

echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js");
SG_
  • 1,316
  • 14
  • 26
  • Awesome! Just wondering how do you pass value on each exec? Cause I need to pass a unique search term. – eozzy Sep 06 '16 at 22:41
  • @3zzy You could pass it as command line arguments `echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js arg1 arg2");` , read more about casperjs command line arguments [here](http://docs.casperjs.org/en/latest/cli.html) – SG_ Sep 07 '16 at 08:54
  • Ah man, a bit late. I spent like a day setting up PHP-Casper which is okay but too slow and doesn't work with `nohup` .. Will try this, thanks! – eozzy Sep 08 '16 at 02:21
3

You can use php-casperjs library which is a simple wrapper for casperjs.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Ravi Misra
  • 196
  • 2
  • 8