PHP implementation through the stomp protocol to connect ActiveMQ operation example

Time:2021-6-10

In this paper, an example is given to show how PHP can connect to ActiveMQ through stomp protocol. The details are as follows:

We have introduced the installation and use of PHP ActiveMQ. Here we will talk about how PHP connects to ActiveMQ through stomp protocol.

1、 Installing stomp extension of PHP


http://pecl.php.net/package/stomp

For example: stomp-2.0.0.tgz


> tar xf stomp-1.0.9.tgz
> cd stomp-1.0.9
> /data/php56/bin/phpize
> ./configure --with-php-config=/data/php56/bin/php-config
> make && make install

Modify php.ini file


> vi /data/php56/lib/php.ini

Pay attention to the extension path of PHP environment here (please modify it yourself)


extension_dir = "/data/php56/lib/php/extensions/no-debug-zts-20131226/"
extension=stomp.so

Check whether the extension is installed successfully


> /data/php56/bin/php -m|grep Stomp

2、 Using stomp extension to connect to ActiveMQ

We passed http://192.168.1.222 : 8161 / admin / create a queue test in the background.

And send a message in the test queue

Then we get the message in the queue through PHP, and the code is as follows:

<?php
try {
 //61613 is the default port, which can be viewed in the file conf / activemq.xml in the ActiveMQ directory
 $stomp = new Stomp('tcp://192.168.1.222:61613');
} catch (StompException $e) {
 die('error: ' . $e->getMessage());
}

//Subscribe to a message queue
$stomp->subscribe('/queue/test');
//Read queue
$frame = $stomp->readFrame();
//Output message body content
echo $frame->body, PHP_EOL;

Run the code above


> /data/php56/bin/php 1.php

For more information about PHP, readers interested in it can see the following topics: PHP extension development tutorial, PHP network programming skills summary, PHP curl usage summary, PHP array operation skills encyclopedia, PHP data structure and algorithm tutorial, PHP programming algorithm summary and PHP string Usage Summary

I hope this article is helpful for PHP programming.