带有 Laravel 的 Php-phantomjs(文件不存在或不可执行:bin/phantomjs)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32015599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:11:23  来源:igfitidea点击:

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

phplaravelcentoslaravel-5.1php-phantomjs

提问by Zakaria Acharki

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

我正在尝试在CentOS 7Windows 8下将php-phantomjs与 Laravel 5一起使用。

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

我按照PHP Phantom 安装(安装成功)中的说明进行操作,之后我在尝试执行基本用法代码时收到此错误:

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)

执行 PhantomJs 程序“默认”时出错 - 文件不存在或不可执行:bin/phantomjs(视图: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:

我在谷歌上搜索了很多,找到并尝试了几种解决方案,但没有成功。在 Stackoverflow 上,我发现了一个问题有人在 ubuntu 环境中成功地将 jonnyw 的“php phantomjs”与 laravel 一起使用了吗?. 我可以在最后一条评论中看到这个人解决了这个问题:

$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)

文件不存在或不可执行:phantomjs.exe(查看: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.

它返回 1,这意味着 PHP 可以找到带有absolute_path.



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

我不知道我做错了什么。任何已经在laravel中成功使用“php phantomjs”的人可以帮助我吗?

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

注意:有问题的代码是 Windows 版本代码,但我在两个操作系统中都收到相同的错误。



UPDATE 1

更新 1

After changing absolute_pathto relative pathit seem like it know phantomjs.exenow, but steel raise same error with phantomloader:

更改absolute_pathrelative path它之后似乎现在知道phantomjs.exe了,但是 steel 引发了同样的错误phantomloader

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

文件不存在或不可执行:../bin/phantomloader(查看:PATH_TO_PROJECT\resources\views\welcome.blade.php)

Tried this solution, but same error :

尝试了这个解决方案,但同样的错误:

$client->setPhantomLoader('../bin/phantomloader');

采纳答案by Aditya Giri

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

好吧,看看这里的视频: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());

回答by Roman Chyrva

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

也许对某人来说,这会有所帮助。新版本的 Php-phantomjs(4.*) 没有方法:

$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');

回答by alexrussell

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

FWIW 我刚刚将它安装在一个非常快速的测试项目中,只需进行一个小的调整就可以正常工作。

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

composer.json按照说明将以下内容添加到我的文件中:

"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 installwill install the library and then install phantomjs afterwards.

然后composer install将安装库,然后安装 phantomjs。

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

然后我将http://jonnnnyw.github.io/php-phantomjs/usage.html#basic-request 中的顶部示例复制并粘贴到index.php(在需要 Composer 的自动加载器之后)。

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.

然后我所要做的(因为我在英国)就是删除响应状态 if(因为我收到 301)以使 Google 主页显示。

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

现在这是在 Mac 上,但我无法想象它在 Centos 上有什么不同。

Then, putting my index.phpfile 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():

然后,将我的index.php文件放在子目录中public,就像在 Laravel 安装中一样,我所要做的就是在之后添加以下行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-dirin composer.jsondoes not always fully update the files that get put in bin. As such, I had to rm -rf bin vendorand then composer updateagain to ensure I had a fresh installation of composer and its packages.

显然这不是一个完整的 Laravel 安装,但它确实模仿了环境。我注意到的一件事是,更改bin-dirincomposer.json并不总是完全更新放入bin. 因此,我只好rm -rf bin vendorcomposer update一次,以确保我有作曲家及其包装的全新安装。

回答by exussum

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

php 以什么用户身份运行?当你发现使用

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

再次检查,它应该仍然有效