12

I'm trying to use php-phantomjs with Laravel 5 under CentOS 7 and Windows 8.

I followed the instructions at PHP Phantom installation (installation done with success), after that I received this error while trying to execute the Basic Usage code:

Error when executing PhantomJs procedure "default" - File does not exist or is not executable: bin/phantomjs (View: PATH_TO_PROJECT\resources\views\welcome.blade.php)


Basic usage code

use JonnyW\PhantomJs\Client;

$client = Client::getInstance();

/** 
 * @see JonnyW\PhantomJs\Message\Request 
 **/
$request = $client->getMessageFactory()->createRequest('http://google.com', 'GET');

/** 
 * @see JonnyW\PhantomJs\Message\Response 
 **/
$response = $client->getMessageFactory()->createResponse();

// Send the request
$client->send($request, $response);

if($response->getStatus() === 200) {

    // Dump the requested page content
    echo $response->getContent();
}

I Googled a lot, finding and trying several solutions, but without success. Here on Stackoverflow, I found one question Anyone successfully used jonnyw's “php phantomjs” with laravel, in a ubuntu envirement?. I can see in the last comment that the guy solved the problem with:

$client->setBinDir('absolute_path/bin');
$client->setPhantomJs('phantomjs.exe');

I triyed that also and it's return another error :

File does not exist or is not executable: phantomjs.exe (View: PATH_TO_PROJECT\resources\views\welcome.blade.php)

But when I try:

echo file_exists('absolute_path/bin/phantomjs.exe');

It returns 1 which means PHP can find the file with absolute_path.


I don't know what I'm doing wrong. Anyone already successfully used “php phantomjs” with laravel who can help me?

NOTE : The code included in question is a windows version code, but i receive the same error in the both OS.


UPDATE 1

After changing absolute_path to relative path it seem like it know phantomjs.exe now, but steel raise same error with phantomloader:

File does not exist or is not executable: ../bin/phantomloader (View: PATH_TO_PROJECT\resources\views\welcome.blade.php)

Tried this solution, but same error :

$client->setPhantomLoader('../bin/phantomloader');
Community
  • 1
  • 1
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101

4 Answers4

11

Well, have a look at the video here: https://www.youtube.com/watch?v=unLTKz9Jqyw

This guy explains all the stuff you might need. Hope that helps.

EDIT:

Try putting this code...

<?php
//test.php

$phantom_loc = "C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\projects\web-optimization\phantomjs4\src\bin\phantomjs.exe";

$dir = "C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\projects\web-optimization\phantomjs4\src\bin\\";

//Is dir executable
$dir_is_Writeable = @file_exists($dir . ".");

if ($dir_is_Writeable === true) {
    echo "$dir is writable";
} else {
    echo "$dir is not writable";
}

echo "<br><br>";
//is executable
if(is_executable($phantom_loc)) {
    echo ("$phantom_loc is executable");
} else {
    echo ("$phantom_loc is not executable");
}

echo "<br><br>";

//Jonnyw
require 'vendor/autoload.php';

use JonnyW\PhantomJs\Client;

$client = Client::getInstance();
$client->setBinDir('C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\projects\web-optimization\phantomjs4\src\bin\\');
$client->setPhantomJs('phantomjs.exe');

var_dump($client->getCommand());
Aditya Giri
  • 1,786
  • 18
  • 33
  • Do you have phantomjs.exe in the place? Or did you even install phnatomjs, because I couldn't find it anywhere on the page that you installed PhantomJS. If not, go http://phantomjs.org/download.html and install it now. – Aditya Giri Aug 18 '15 at 09:45
  • Yes, already mentioned in question "I followed the instructions at PHP Phantom installation (installation done with success)". – Zakaria Acharki Aug 18 '15 at 10:21
  • Do you have phantomJS.exe in the same directory? If not try to put it. Because the code is working fine for me. – Aditya Giri Aug 19 '15 at 13:37
  • I have the phantomJS.exe in /bin directory. – Zakaria Acharki Aug 19 '15 at 13:38
  • is phantomloader in the same directory – Aditya Giri Aug 19 '15 at 13:39
  • Yes i have 3 files in `/bin` directory (phantomjs.exe + phantomloader + phantomloader.bat). – Zakaria Acharki Aug 19 '15 at 13:42
  • Check out my edit and give me the output of that file – Aditya Giri Aug 19 '15 at 13:43
  • if i comment line `var_dump($client->getCommand());` i have `../bin is writable & ../bin/phantomjs.exe is executable` if not i get **File does not exist or is not executable: ../bin/phantomloader**... – Zakaria Acharki Aug 19 '15 at 13:51
  • @ZakariaAcharki Well as I am a linux user, I cannot say what exact process is, but I do something called `chmod` to get the file writable. Maybe you know about windows. Try to make the file editable. – Aditya Giri Aug 19 '15 at 14:02
9

Maybe for someone, it will be helpful. The new version of Php-phantomjs(4.*) has no methods:

$client->setBinDir('absolute_path/bin');
$client->setPhantomJs('phantomjs.exe');

So, instead of it, you can use that:

$client->getEngine()->setPath('absolute_path/bin/phantomjs.exe');
Roman Chyrva
  • 101
  • 1
  • 3
  • 1
    to get this to work with Symfony I used the relative path $phantom->getEngine()->setPath('../bin/phantomjs'); – Acyra Aug 24 '16 at 00:47
3

FWIW I just installed this on a very quick test project and it worked fine with only one small tweak.

I added the following to my composer.json file as per the instructions:

"require": {
    "jonnyw/php-phantomjs": "~3.0"
},
"config": {
    "bin-dir": "bin"
},
"scripts": {
    "post-install-cmd": [
        "PhantomInstaller\\Installer::installPhantomJS"
    ],
    "post-update-cmd": [
        "PhantomInstaller\\Installer::installPhantomJS"
    ]
}

Then composer install will install the library and then install phantomjs afterwards.

Then I copied and pasted the top example from http://jonnnnyw.github.io/php-phantomjs/usage.html#basic-request into index.php (after requiring Composer's autoloader).

All I then had to do (because I'm in the UK) was remove the response status if (as I get a 301) to make the Google homepage show.

Now this was on a Mac but I can't imagine it being any different on Centos.

Then, putting my index.php file inside a subdirectory public, as you would have in a Laravel install, all I had to do was add the following line after Client::getInstance():

$client->setBinDir('../bin');

Then it worked again.

Obviously this is not a full Laravel install, but it does mimic the environment alright. One thing I did notice was that changing the bin-dir in composer.json does not always fully update the files that get put in bin. As such, I had to rm -rf bin vendor and then composer update again to ensure I had a fresh installation of composer and its packages.

alexrussell
  • 13,856
  • 5
  • 38
  • 49
  • Ok i'll try do the same thing and reinstall it from scratch. – Zakaria Acharki Aug 19 '15 at 15:19
  • Maybe first just `rm -rf bin vendor` and then `composer update`. That should give you a fresh Composer installation. But do make sure you have the `bin-dir` thing in `composer.json` as well as setting the `$client`'s bin dir to `../bin`. – alexrussell Aug 19 '15 at 15:28
3

The error states

File does not exist or is not executable:

You have shown the file exists. Now make it executable

chmod +x /path/to/bin/phantomloader

And try again

If this works you need to lock down some of the permissions

What user is php running as ? When you find out use

chmod -x /path/to/bin/phantomloader
chown phpuser  /path/to/bin/phantomloader
chmod u+x  /path/to/bin/phantomloader

Check again and it should still work

exussum
  • 18,275
  • 8
  • 32
  • 65