0
votes

I am trying to pass CasperJS results to PHP but it's not working (the output is empty).

Here is my code:

<?php

exec("casperjs test.js", $array);

print_r($array);

?>

The exec works for any other command ("echo hello"), but not casperjs. $array is empty and execution time is instantaneous...

The PHP script is of course in the same directory as test.js

I'm guessing it's a path issue but nothing works... I've tried "/usr/local/bin/casperjs test.js", it returns

Fatal: [Errno 2] No such file or directory; did you install phantomjs?

Any ideas?

1
It is actually... Sorry about that. I added these two lines to fix the issue: "putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs"); putenv("DYLD_LIBRARY_PATH");" - Nelty
@Nelty Since the duplicated question does not provide your answer, please provide your own answer and accept it in 8 hours if it fully solves your problem. - Artjom B.
Hey, just be careful launching your scraper the way you're trying to launch it, casper by default tends to be verbose, and if you're planning to parse whatever the scraper outputs, you might end up parsing the wrong things. It's much more better if at the end of your scraping cycle, you make casper "visit" a website, which is nothing else but an endpoint of your program, and make casper send a POST package with the information you want to that endpoint, so that you can store it and make sure that the information you're getting is well formatted. - ILikeTacos
@AlanChavez: how to I send a POST package with Casper? - Nelty

1 Answers

1
votes

To solve this issue, I had to add these two lines at the beginning of my PHP file:

putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
putenv("DYLD_LIBRARY_PATH");

And now it works!