制作者的介绍仅有上面的一句话。 更多详情请阅读仓库 README。 仓库 README ↗
正在加载完整 README。
查看完整 README ↗This library is a pure PHP implementation of the AMQP 0-9-1 protocol. It's been tested against RabbitMQ.
The library was used for the PHP examples of RabbitMQ in Action and the official RabbitMQ tutorials.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Thanks to videlalvaro and postalservice14 for creating php-amqplib.
The package is now maintained by Ramūnas Dronga, Luke Bakken and several VMware engineers working on RabbitMQ.
Starting with version 2.0 this library uses AMQP 0.9.1 by default and thus requires RabbitMQ 2.0 or later version.
Usually server upgrades do not require any application code changes since
the protocol changes very infrequently but please conduct your own testing before upgrading.
Since the library uses AMQP 0.9.1 we added support for the following RabbitMQ extensions:
Extensions that modify existing methods like alternate exchanges are also supported.
enqueue/amqp-lib is a amqp interop compatible wrapper.
AMQProxy is a proxy library with connection and channel pooling/reusing. This allows for lower connection and channel churn when using php-amqplib, leading to less CPU usage of RabbitMQ.
Ensure you have composer installed, then run the following command:
composer require php-amqplib/php-amqplib
That will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your .php files in order to use the library
require_once __DIR__.'/vendor/autoload.php';
Then you need to use the relevant classes, for example:
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
With RabbitMQ running open two Terminals and on the first one execute the following commands to start the consumer:
cd php-amqplib/demo
php amqp_consumer.php
Then on the other Terminal do:
cd php-amqplib/demo
php amqp_publisher.php some text to publish
You should see the message arriving to the process on the other Terminal
Then to stop the consumer, send to it the quit message:
php amqp_publisher.php quit
If you need to listen to the sockets used to connect to RabbitMQ then see the example in the non blocking consumer.
php amqp_consumer_non_blocking.php
Please see CHANGELOG for more information what has changed recently.
http://php-amqplib.github.io/php-amqplib/
To not repeat ourselves, if you want to learn more about this library, please refer to the official RabbitMQ tutorials.
amqp_ha_consumer.php: demos the use of mirrored queues.amqp_consumer_exclusive.php and amqp_publisher_exclusive.php: demos fanout exchanges using exclusive queues.amqp_consumer_fanout_{1,2}.php and amqp_publisher_fanout.php: demos fanout exchanges with named queues.amqp_consumer_pcntl_heartbeat.php: demos signal-based heartbeat sender usage.basic_get.php: demos obtaining messages from the queues by using the basic get AMQP call.If you have a cluster of multiple nodes to which your application can connect,
you can start a connection with an array of hosts. To do that you should use
the create_connection static method.
For example:
$connection = AMQPStreamConnection::create_connection([
['host' => HOST1, 'port' => PORT, 'user' => USER, 'password' => PASS, 'vhost' => VHOST],
['host' => HOST2, 'port' => PORT, 'user' => USER, 'password' => PASS, 'vhost' => VHOST]
],
$options);
This code will try to connect to HOST1 first, and connect to HOST2 if the
first connection fails. The method returns a connection object for the first
successful connection. Should all connections fail it will throw the exception
from the last connection attempt.
See demo/amqp_connect_multiple_hosts.php for more examples.
Let's say you have a process that generates a bunch of messages that are going to be published to the same exchange using the same routing_key and options like mandatory.
Then you could make use of the batch_basic_publish library feature. You can batch messages like this:
$msg = new AMQPMessage($msg_body);
$ch->batch_basic_publish($msg, $exchange);
$msg2 = new AMQPMessage($msg_body);
$ch->batch_basic_publish($msg2, $exchange);
and then send the batch like this:
$ch->publish_batch();
以上为 README 的开头部分。其余内容请阅读原文。 查看完整 README ↗