简体   繁体   中英

Php Can't find Stomp class

I'm trying to get a queue from activemq using a PHP client. I included in the project a stomp library. Here is my project tree:

TestPhp/
├── PhpInfo.php
├── SimpleStompConsumer.php
└── Stomp
    ├── Exception
    |   └── StompException.php
    ├── ExceptionInterface.php
    ├── Frame.php
    ├── Message
    │   ├── Bytes.php
    │   └── Map.php
    ├── Message.php
    └── Stomp.php

and here is my code

<?php
$path = "/var/www/test/TestPhp/Stomp/Stomp.php";

require_once($path);
$con = new Stomp("tcp://localhost:61613");
$con->connect();
$con->subscribe("/queue/test");
$msg = $con->readFrame();


if( $msg != null) {
    echo "Received message with body '$msg->body'\n";
    $con->ack($msg);
} else {
echo "Failed to receive a message\n";
}
$con->disconnect();

?>

When I run the php file I get the following error and I don' know what to do. PHP Fatal error: Class 'Stomp' not found in /var/www/test/TestPhp/SimpleStompConsumer.php on line 5 PHP Stack trace: PHP 1. {main}() /var/www/test/TestPhp/SimpleStompConsumer.php:0

I'd appreciate any directives. Thanks.

Edit:  

Library is called Stomp. Here is the github link and the download link

Edit:

Change $con = new Stomp("tcp://localhost:61613"); to $con = new FuseSource\\Stomp\\Stomp("tcp://localhost:61613"); $con = new FuseSource\\Stomp\\Stomp("tcp://localhost:61613");

ERROR returned: 

PHP Fatal error: Class 'FuseSource\\Stomp\\Exception\\StompException' not found in /var/www/test/TestPhp/Stomp/Stomp.php on line 174 PHP Stack trace: PHP 1. {main}() /var/www/test/TestPhp/SimpleStompConsumer.php:0 PHP 2. FuseSource\\Stomp\\Stomp->connect() /var/www/test/TestPhp/SimpleStompConsumer.php:6 PHP 3. FuseSource\\Stomp\\Stomp->_makeConnection() /var/www/test/TestPhp/Stomp/Stomp.php:195

Try

$con = new FuseSource\Stomp\Stomp("tcp://localhost:61613");

(namespace).

Edit: One "Stomp" was missing.

Stomp library is a composer package, so you need composer class loader or any other PSR-0 compatible, see this https://getcomposer.org/doc/01-basic-usage.md .

Example PSR-0 class loader from PHP-Fig: https://gist.github.com/jwage/221634

require_once 'path_to_spl_class_loader.php';
$loader = new SplClassLoader('FuseSource\Stomp', __DIR__.'/Stomp');
$loader->register();
new FuseSource\Stomp\Stomp();

Besides it's namespaced class, add this line to you file:

use FuseSource\Stomp\Stop;
new Stomp();

Or use

new FuseSource\Stomp\Stomp();

For me the issue was that stomp.so was not registered in my PHP engine in Apache. I had used pear to install stomp which correctly registered it in my command line PHP engine.

The PHP engine in my Apache is using a /etc/php.ini file separate from /usr/local/etc/php/5.5/php.ini used by the PHP engine running on my local command line. As far as I can tell, this is the recommended configuration (see Change php.ini location )

To remedy I needed to add extension="stomp.so" to the /etc/php.ini file used by my Apache service and then restart Apache.

My code does not have a require_once . I simply invoke $con = new Stomp('tcp://localhost:61613', $user, $password); directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM