首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP中加载Pheanstalk?

如何在PHP中加载Pheanstalk?
EN

Stack Overflow用户
提问于 2014-10-01 10:56:52
回答 2查看 5.9K关注 0票数 3

我正在尝试让Pheanstalk在PHP上工作,但我无法加载它。

我从https://github.com/pda/pheanstalk下载了源代码,将src/Pheanstalk移到我的项目目录中,然后在test.php中执行以下操作

代码语言:javascript
复制
use Pheanstalk\Pheanstalk;
$pheanstalk = new Pheanstalk('127.0.0.1');

但这会给出以下错误:

代码语言:javascript
复制
Fatal error: Class 'Pheanstalk\Pheanstalk' not found in test.php on line 2

如何使用克隆的git库中的Pheanstalk?

EN

回答 2

Stack Overflow用户

发布于 2014-10-03 14:53:44

我写了一篇关于豆茎,豆茎和鸡茎的文章;

查看:How to install Beanstalkd and Pheanstalk on Ubuntu

你的问题的解决方案就在那里。

1)。使用Virtual Machine安装Ubuntu DesktopServer

我在这个例子中使用了Oracle VM VirtualBox

确保已设置Bridged Network Connection

根据要求设置用户名和密码。

我使用:

用户名:william密码:123456

2)。启动Ubuntu并登录

3)。以超级用户身份登录

使用:sudo su,请求时输入超级用户密码(我的密码是: 123456 );

4)。安装Open SSH并通过Putty访问Ubuntu virtual machine

文档:https://help.ubuntu.com/10.04/serverguide/openssh-server.html

用法:apt-get install openssh-client

安装客户端,然后

用法:apt-get install openssh-server

安装服务器;

5)。安装Apache2 web服务器

用法:apt-get install apache2

现在,在浏览器中键入以下内容:

http://localhost

和你的虚拟机ip (我的是192.168.1.104 )

6)。安装MySQL

用法:apt-get install mysql-server php5-mysql

设置MySQL root用户密码: 123456重复密码: 123456

7)。安装PHP5

用法:apt-get install php5 libapache2-mod-php5 php5-mcrypt

8)。安装cURL

用法:apt-get install curl

9)。安装Composer

官网:https://getcomposer.org/文档:https://getcomposer.org/doc/00-intro.md (快速入门)

use:curl -s http://getcomposer.org/installer | php或use:curl -sS https://getcomposer.org/installer | php现在,必须移动和转换composer.phar

用法:mv composer.phar /usr/bin/composer

10)。安装Beanstalkd

用法:apt-get install beanstalkd

现在,让我们确保beanstalkd persistent mode is active

ps轴

ps ax | grep bean

查找beanstalkd.conf

更新数据库

查找beanstalkd.conf

nano /etc/default/beanstalk

uncomment last line in order to save persistent mode to active

11)。安装Pheanstalk

文档:https://github.com/pda/pheanstalk

更改目录使用:cd /var/www/html

创建一个新目录: /var/www/html/pheanstalk_test使用:mkdir pheanstalk_test

使用:cd pheanstalk_test将目录更改为新创建的目录

使用:nano composer.json在这个新目录下创建一个composer.json文件

在文件中写入以下数据:{ "require":{ "pda/pheanstalk":"v3.0.0" } }

并保存文件(按Ctrl+X键,按Y键,按ENTER键)

用法:composer update

现在,应该开始下载供应商文件夹数据

或者使用:git clone https://github.com/pda/pheanstalk.git

示例:如何将数据放在tube/s上

<?php include 'vendor/autoload.php'; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('127.0.0.1'); while(true){ $tube_id=rand(1,9); $r1=rand(1,10000000); $r2=rand(1,10000000); $pheanstalk->useTube('testtube'.$tube_id)->put('{'.$r1.':'.$r2.'}'); } ?>

使用: php put.php和一个put进程将启动信息!打开的实例越多,放在管道上的数据就越多

如何从tube/s获取数据

<?php include 'vendor/autoload.php'; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('127.0.0.1'); while(true){ $tube_id=rand(1,9); $job=$pheanstalk->watch('testtube'.$tube_id)->ignore('default')->reserve(); if($job){ echo $job->getdata(); $pheanstalk->delete($job); } } ?>

用法: php get.php和get进程将启动信息!打开的实例越多,处理的数据就越多;

12)。安装Beanstalk Console

文档:https://github.com/ptrofimov/beanstalk_console

更改目录

用法:cd /var/www/html

创建新文件夹

用法:mkdir beanstalk_console

用法:git clone https://github.com/ptrofimov/beanstalk_console.git

更改文件的权限: storage.json chmod 777 storage.json

现在,要访问beanstalk console,请在浏览器中编写:http://localhost/beanstalk_console/public

13)。就是这个!

票数 5
EN

Stack Overflow用户

发布于 2017-06-22 10:08:56

虽然Ionut的答案对于让Pheanstalk工作是可行的,但它实际上并没有回答提出的问题:如何从项目代码库的git克隆中让Pheanstalk工作?(这里隐含的意思是您不想使用Composer进行安装,这是我将在此答案中使用的假设。)

主要的问题是,您不能只使用require('src/Pheanstalk.php');,然后开始使用Pheanstalk类。Pheanstalk库在很大程度上依赖于PHP类自动加载,因此如果不进行设置,它将会像原始问题中描述的那样出错。

如果你的项目已经有了一个兼容PSR-4的自动加载器,那就太棒了!将它指向composer.json文件的psr-4部分,看看它的神奇之处。

但是,对于其他所有人,您需要使用Thibault提供的函数来回答类似的问题:https://stackoverflow.com/a/39774973

假设您已经将Pheanstalk git repo克隆到您的pheanstalk项目中,那么您只需使用loadPackage('pheanstalk'),它应该都可以正常工作。

完整示例:

代码语言:javascript
复制
function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpath) {
        spl_autoload_register(function ($classname) use ($namespace, $classpath, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\\\#", "/", $classname).".php";
                include_once $dir."/".$classpath."/$filename";
            }
        });
    }
} 

loadPackage(__DIR__."/pheanstalk");

use Pheanstalk\Pheanstalk;

$pheanstalk = new Pheanstalk('127.0.0.1');

echo $pheanstalk->getConnection()->isServiceListening();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26133424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档