Php Can't find Stomp class

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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

回答1:

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();


回答2:

Try

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

(namespace).

Edit: One "Stomp" was missing.



回答3:

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



转载请标明出处:Php Can't find Stomp class
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!