Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php-phantomjs with Laravel (File does not exist or is not executable: bin/phantomjs)

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');
like image 918
Zakaria Acharki Avatar asked Aug 14 '15 17:08

Zakaria Acharki


4 Answers

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());
like image 141
Aditya Giri Avatar answered Nov 10 '22 01:11

Aditya Giri


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');
like image 41
Roman Chyrva Avatar answered Nov 10 '22 02:11

Roman Chyrva


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.

like image 3
alexrussell Avatar answered Nov 10 '22 02:11

alexrussell


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

like image 3
exussum Avatar answered Nov 10 '22 01:11

exussum