alwex / php-casperjs

simple PHP wrapper for CasperJS
MIT License
182 stars 68 forks source link

Can not find CasperJS #25

Open casas111 opened 8 years ago

casas111 commented 8 years ago

Hello, I'm having trouble running the example code.

I installed phantomjs and casperjs.

I installed php-casperjs

And when I run the example I get:

Fatal error: Class 'Browser\Casper' not found in C:\wamp\www\crawler\ajax.php on line 5

When I removed the: namespace Browser; from Casper.php and included Casper.php manually: include 'vendor/phpcasperjs/phpcasperjs/src/Browser/Casper.php';

I get:

Fatal error: Uncaught exception 'Exception' with message 'Can not find CasperJS.' in C:\wamp\www\crawler\vendor\phpcasperjs\phpcasperjs\src\Browser\Casper.php on line 560 ( ! ) Exception: Can not find CasperJS. in C:\wamp\www\crawler\vendor\phpcasperjs\phpcasperjs\src\Browser\Casper.php on line 560

Finally I went through the Casper.php file and changed this: private $path2casper = '/usr/local/bin/'; //path to CasperJS for all paths I could think of without any success.

Could you please help me? thankyou

alwex commented 8 years ago

hello, it seems that Casper class is not finding your local casperjs installation or your temp dir is not configured correctly. If you are using windows, you should create your Casper instance like

$casper = new Casper('C:\\your-path-to-casperjs', 'C:\\your-temp-dir');

If it is still not working, can you check the file created in your temp directory, you should be able to run it this way:

casperjs /your-tmp-dir/yourscript.js
casas111 commented 8 years ago

Thanks, what is the temp directory you are referring to?

alwex commented 8 years ago

The temp directory is the directory where the plain javascript casperjs files are created during the session. The script is created during $casper->run(), if the execution failed, the javascript file should still be in the temp directory. You can specify this directory in the constructor, see my previous comment.

nikhilsuccessive commented 8 years ago

Hello Alwex Thanks for such a awesome repo of Casper and PHP .I am going through one problem that everytime i run my script it says that CasperJS is not installed.Is there any idea what i am missing ?

Thanks

alwex commented 8 years ago

Hello nikhilsuccessive,

Check if casperjs executable is available in the path of the user that will execute the php code.

nikhilsuccessive commented 8 years ago

@alwex Thanks for replying fast.Now its upto you please save my job.

when i am running on server it showing this error "Can not find CasperJS in path: /usr/bin/" when my casperJs is installed on same path .If i am running /usr/bin/casperjs then its working fine.

I will let you know in a code where the problem is occurring . open Casper.php go to line 589 where it is written throw new \Exception('Can not find CasperJS.');

I am wondering that why output is zero when it is creating file in /tmp folder but unable to load file .

When i am directly running /usr/bin/casperjs /tmp/php-casperjs-SSGeG on terminal .

It showing this error **_Unable to open file: /tmp/php-casperjs-SSGeG

phantomjs://code/bootstrap.js:110 in __die Unable to load script /tmp/php-casperjs-SSGeG; check file syntax

phantomjs://code/bootstrap.js:110 in _die**

I am working on UBUNTU.Do consider this request please.

Thanks

nikhilsuccessive commented 8 years ago

Hello @alwex When i am directly running /usr/bin/casperjs /tmp/php-casperjs-SSGeG on terminal .

It showing this error **_Unable to open file: /tmp/php-casperjs-SSGeG

phantomjs://code/bootstrap.js:110 in __die**

I think phantoms is not getting loaded on exec() function due to this its returning empty array.

Thanks

nikhilsuccessive commented 8 years ago

Hello @alwex

public function start($url)
        {
                $this->_clear();
                $step = <<<FRAGMENT
                var casper = require('casper').create({
                        verbose: false,
                        logLevel: 'debug',
                        colorizerType: 'Dummy',
                        pageSettings: {
                                loadImages:false
                        }
                });
                //casper.userAgent('Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1');
                //phantom.setProxy('ltpc170d53a:c170d53a@ltp.viia.me','8945');
                casper.start().then(function() {
                        this.open('$url', {
                         headers: {
                         'Accept': 'text/html'
                        }
                     });
                });
                casper.userAgent('Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1');
FRAGMENT;

                $this->addStep($step);

                return $this;
        }

Please check whether i am setting userAgent on a correct position and is there anyway to check user-agent is setting fine or not.

Thanks for awesomeness

antoiba86 commented 7 years ago

Hello there,

I had this exact error and I was like running crazy to solve it because any solution found here or on stackoverflow didn't work for me. Until I read this answer. http://stackoverflow.com/a/30137893

It happens that some frameworks for example cakephp, doesn't get correctly the vendors directory. I solved putting the following code: require_once "../../vendors/autoload.php";

I mean, you must have the autoload.php file to point at, and now the error won't happen. I didn't try casperjs but my web is running error clean now.

haisamhameed commented 7 years ago

was stuck on this error "Can not find CasperJS" then i changed setpath2casperjs to absolute path and run the script and its in execution from last one hour

antoiba86 commented 7 years ago

Ok, maybe it's not working for you, but for ubuntu distributions there is a problem with the phantomjs. You need to download phantomjs from the repository at bitbucket. Saw here https://github.com/ariya/phantomjs/issues/14376#issuecomment-246310115

@3zzy the steps I used are as follows: wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2 tar -xvf phantomjs-2.1.1-linux-x86_64.tar cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/phantomjs

Does this help your case?

rsyr commented 7 years ago

@antoiba86 I attempted this as i'm using Ubuntu and unfortunately it didn't work either (although it may have contributed to me finding a successful workaround afterwards so i'd recommend trying this as well). My debug steps are below hopefully they might help someone else.

I noticed that on my Laravel/Homestead box the path2casper was:

/usr/local/bin/casperjs

When running casperjs on terminal it was actually running in different directory:

CasperJS version 1.1.3 at /usr/lib/node_modules/casperjs, using phantomjs version 2.1.1

Changing the path2casper to the above didn't work I echoed out exec() command and ran it in terminal and received an error telling me that casperjs was a directory, I navigated to the directory and found that my casperJS install was actually within the bin of that folder:

/usr/lib/node_modules/casperjs/bin/casperjs

Changing the following lines in my Casper.php finally did the trick:

$this->path2casper = '/usr/lib/node_modules/'; //new path to CasperJS exec($this->path2casper . 'casperjs/bin/casperjs ' . $filename . $options, $output);

natterstefan commented 7 years ago

Hello everbody,

I have a similar issue on my macOS Sierra system. I tried to install php-casperjs according to the Readme but did not succeed. You find my question on StackOverflow. Maybe one of you guys can help me there.

antoiba86 commented 7 years ago

@natterstefan Ok, you can do a few things to know what happen. Try looking for the route of casperjs is correctly set. Later, it is not. Go to your framework error.log and search for some error. If this it's not possible or there is not anything.

You should go to your apache error.log or whatever systems use macOS. On Ubuntu for example, the directory is /var/log/apache2/error.log If there is some problem with phantomjs, then the problem is with your phantomjs version. So try installing phantomjs with bitbucket as I said on another post. Sorry but I cannot post on stackoverflow.

natterstefan commented 7 years ago

@antoiba86 Hello, thanks for your reply. Actually I was able to solve it. I do not what went wrong during the installation of casperjs or phantomjs but I deinstalled them (both with npm and brew) and installed them again. Then it worked.

AdrianT95 commented 6 years ago

Hi there @antoiba86 @alwex I hope any of you can help me. I have the same issue, I pasted the example to a PHP file and when I open it, I get this error: https://i.imgur.com/lrA8tin.png

phantomjs and casperjs are completely installed on my computer because I can run casperjs files and they work perfectly on my end: https://i.imgur.com/awTgIYL.png

For some reason, php-casperjs is not working for me and I already run composer require phpcasperjs/phpcasperjs, the above answers didn't help me to fix the issue.

Could any of you help me, please? ~ I have Windows 10 and I am using XAMPP v3.2.2.

Thank you in advance!

antoiba86 commented 6 years ago

Your script is not taken correctly the path to casperjs-php. Maybe, you can include the absolute path to the composer autoload.pho to verify is that.

AdrianT95 commented 6 years ago

Thank you for responding @antoiba86, so I already have this included and it doesn't fix the issue, here's the screenshot of it: https://i.imgur.com/LgCxUQf.png maybe is there any mistake?

antoiba86 commented 6 years ago

The PHP script is in the same folder as the autoload file?

AdrianT95 commented 6 years ago

Yes, because if not, there would be an error saying something like "autoload.php can't be found", but the error is: Class 'Browser\Casper' not found in C:\xampp\htdocs\web scraping\web scraping.php on line 4

AdrianT95 commented 6 years ago

Do you think I should try what natterstefan did? So maybe a clean installation of everything will fix it.

AdrianT95 commented 6 years ago

Ok I think I fixed it by formatting my computer & installing windows again. But this is what I get now:

Notice: tempnam(): file created in the system's temporary directory in C:\Users\User\vendor\phpcasperjs\phpcasperjs\src\Casper.php on line 595

Fatal error: Uncaught Exception: Can not find CasperJS. in C:\Users\User\vendor\phpcasperjs\phpcasperjs\src\Casper.php:607 Stack trace: #0 C:\xampp\htdocs\Web Scraping\Web Scraping.php(70): Browser\Casper->run() #1 {main} thrown in C:\Users\User\vendor\phpcasperjs\phpcasperjs\src\Casper.php on line 607

Any help?

antoiba86 commented 6 years ago

That is because, casperjs is not a environment variable and cannot use casperjs.

AdrianT95 commented 6 years ago

Oh so is there a way to fix it? I just added require_once "C:/Users/User/vendor/autoload.php"; as you suggested me before, if I delete it, those errors are gone but it'll say Fatal error: Class 'Browser\Casper' not found again.

AdrianT95 commented 6 years ago

Ok, I don't need more help... Due to these issues, I tried to find other ways to run CasperJS on PHP and I've just found this and it worked for me: https://stackoverflow.com/questions/25001258/cannot-pass-casperjs-result-back-to-php

Anyways, the developer of php-casperjs, should fix all those bugs soon. Thank you for all your help @antoiba86