How do I load Pheanstalk in PHP?
I am trying to get Pheanstalk to work in PHP, but I cannot load it.
I downloaded the source code from https://github.com/pda/pheanstalk
, I moved src / Pheanstalk to my project directory and then did the following to test.php
:
use Pheanstalk\Pheanstalk;
$pheanstalk = new Pheanstalk('127.0.0.1');
But this gives me the following error:
Fatal error: Class 'Pheanstalk\Pheanstalk' not found in test.php on line 2
How can I use Pheanstalk from a cloned git repository?
source to share
I wrote an article on Beanstalk, Beanstalkd, and Pheanstalk;
Check it out: How to Install Beanstalkd and Pheanstalk on Ubuntu
There is a solution to your problem.
1). Install Ubuntu Desktop
or Server
using Virtual Machine
.
I used Oracle VM VirtualBox
for this example.
Make sure it is installed Bridged Network Connection
.
Provide a username and password when prompted.
I used:
username: william
password:123456
2). Start Ubuntu and login
3). Login as superuser
use: sudo su
and enter the superuser password when prompted (mine: 123456);
4). Install Open SSH
and sign in Ubuntu virtual machine
via Putty
.
docs: https://help.ubuntu.com/10.04/serverguide/openssh-server.html
use: apt-get install openssh-client
to install the client and then
use: apt-get install openssh-server
to install the server;
five). Install the Apache2
web server
use: apt-get install apache2
now enter the following in your browser:
http://localhost
and your virtual computer ip (mine was 192.168.1.104
)
6). InstallMySQL
use: apt-get install mysql-server php5-mysql
set MySQL root
user password : 123456 repeat password: 123456
7). InstallPHP5
use: apt-get install php5 libapache2-mod-php5 php5-mcrypt
8). InstallcURL
use: apt-get install curl
nine). InstallComposer
official site: https://getcomposer.org/
docs: https://getcomposer.org/doc/00-intro.md
(Getting Started)
use: curl -s http://getcomposer.org/installer | php
or use: curl -sS https://getcomposer.org/installer | php
now composer .phar should be moved and transformed
use: mv composer.phar /usr/bin/composer
ten). InstallBeanstalkd
use: apt-get install beanstalkd
now, make sure beanstalkd persistent mode is active
ps ax
ps ax | grep beans
find beanstalkd.conf
updatedb
find beanstalkd.conf
nano /etc/default/beanstalk
uncomment last line in order to save persistent mode to active
eleven). InstallPheanstalk
docs: https://github.com/pda/pheanstalk
change directory usage: cd /var/www/html
create a new directory: / var / www / html / pheanstalk_test usage: mkdir pheanstalk_test
change directory to newly created directory using: cd pheanstalk_test
create composer.json file in this new directory using: nano composer.json
write the following data to the file:
{
"require":{
"pda/pheanstalk":"v3.0.0"
}
}
and save the file (press Ctrl + X, press Y, press ENTER)
use: composer update
now vendor folder data should start loading
or use: git clone https://github.com/pda/pheanstalk.git
examples: how to put data on a 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.'}');
}
?>
use: php put.php and the application process will begin Information! the more instances are opened, the more data is superimposed on the tube / s
how to get data from 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);
}
}
?>
use: php get.php and the process of getting Info starts! the more instances are opened, the more data is processed;
12). InstallBeanstalk Console
docs: https://github.com/ptrofimov/beanstalk_console
change directory
use: cd /var/www/html
create a new folder
use: mkdir beanstalk_console
use: git clone https://github.com/ptrofimov/beanstalk_console.git
change file permissions: storage.json
chmod 777 storage.json
now to access Beanstalk Console
, write in the browser:http://localhost/beanstalk_console/public
13). It's him!
source to share
While Ionut's answer is viable for getting Pheanstalk to work, it doesn't actually answer the question posed: how do you get Pheanstalk working from a git clone of a project's repo? (It's implicit here that you don't want to use Composer to install, which is the assumption I'll be using for this answer.)
The main problem is that you can't just require('src/Pheanstalk.php');
and then start using the Pheanstalk class. The Pheanstalk library relies heavily on PHP class autoloading, so without this setting it will fail as described in the original question.
If you have a PSR-4 compatible autoloader for your project, great! Point it to the psr-4 section of the composer.json file and see the magic work.
For everyone else, though, you'll want to use the function provided by Thibault in his answer to a similar question: fooobar.com/questions/493317 / ...
Assuming your project has the Pheanstalk git repository cloned in pheanstalk
, you can just use loadPackage('pheanstalk')
and everything should work correctly.
Complete example:
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 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();
source to share