# php-amqplib #
![PHPUnit tests](https://github.com/php-amqplib/php-amqplib/workflows/PHPUnit%20tests/badge.svg)
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
[![Software License][ico-license]](LICENSE)
[![codecov](https://codecov.io/gh/php-amqplib/php-amqplib/branch/master/graph/badge.svg?token=tgeYkUsaDM)](https://codecov.io/gh/php-amqplib/php-amqplib)
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
This library is a _pure PHP_ implementation of the [AMQP 0-9-1 protocol](http://www.rabbitmq.com/tutorials/amqp-concepts.html).
It's been tested against [RabbitMQ](http://www.rabbitmq.com/).
The library was used for the PHP examples of [RabbitMQ in Action](http://manning.com/videla/) and the [official RabbitMQ tutorials](http://www.rabbitmq.com/tutorials/tutorial-one-php.html).
Please note that this project is released with a [Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
## Project Maintainers
Thanks to [videlalvaro](https://github.com/videlalvaro) and [postalservice14](https://github.com/postalservice14) for creating `php-amqplib`.
The package is now maintained by [Ramūnas Dronga](https://github.com/ramunasd), [Luke Bakken](https://github.com/lukebakken) and several VMware engineers working on RabbitMQ.
## Supported RabbitMQ Versions ##
Starting with version 2.0 this library uses `AMQP 0.9.1` by default and thus requires [RabbitMQ 2.0 or later version](http://www.rabbitmq.com/download.html).
Usually server upgrades do not require any application code changes since
the protocol changes very infrequently but please conduct your own testing before upgrading.
## Supported RabbitMQ Extensions ##
Since the library uses `AMQP 0.9.1` we added support for the following RabbitMQ extensions:
* Exchange to Exchange Bindings
* Basic Nack
* Publisher Confirms
* Consumer Cancel Notify
Extensions that modify existing methods like `alternate exchanges` are also supported.
### Related libraries
* [enqueue/amqp-lib](https://github.com/php-enqueue/amqp-lib) is a [amqp interop](https://github.com/queue-interop/queue-interop#amqp-interop) compatible wrapper.
* [AMQProxy](https://github.com/cloudamqp/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.
## Setup ##
Ensure you have [composer](http://getcomposer.org) installed, then run the following command:
```bash
$ 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
```php
require_once __DIR__.'/vendor/autoload.php';
```
Then you need to `use` the relevant classes, for example:
```php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
```
## Usage ##
With RabbitMQ running open two Terminals and on the first one execute the following commands to start the consumer:
```bash
$ cd php-amqplib/demo
$ php amqp_consumer.php
```
Then on the other Terminal do:
```bash
$ 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:
```bash
$ 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.
```bash
$ php amqp_consumer_non_blocking.php
```
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## API Documentation ##
http://php-amqplib.github.io/php-amqplib/
## Tutorials ##
To not repeat ourselves, if you want to learn more about this library,
please refer to the [official RabbitMQ tutorials](http://www.rabbitmq.com/tutorials/tutorial-one-php.html).
## More Examples ##
- `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.
## Multiple hosts connections ##
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:
```php
$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.
## Batch Publishing ##
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:
```php
$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:
```php
$ch->publish_batch();
```
### When do we publish the message batch? ###
Let's say our program needs to read from a file and then publish one message per line. Depending on the message size, you will have to decide when it's better to send the batch.
You could send it every 50 messages, or every hundred. That's up to you.
## Optimized Message Publishing ##
Another way to speed up your message publishing is by reusing the `AMQPMessage` message instances. You can create your new message like this:
```php
$properties = array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT);
$msg = new AMQPMessage($body, $properties);
$ch->basic_publish($msg, $exchange);
```
Now let's say that while you want to change the message body for future messages, you will keep the same properties, that is, your messages will still be `text/plain` and the `delivery_mode` will still be `AMQPMessage::DELIVERY_MODE_PERSISTENT`. If you create a new `AMQPMessage` instance for every published message, then those properties would have to be re-encoded in the AMQP binary format. You could avoid all that by just reusing the `AMQPMessage` and then resetting the message body like this:
```php
$msg->setBody($body2);
$ch->basic_publish($msg, $exchange);
```
## Truncating Large Messages ##
AMQP imposes no limit on the size of messages; if a very large message is received by a consumer, PHP's memory limit may be reached
within the library before the callback passed to `basic_consume` is called.
To avoid this, you can call the method `AMQPChannel::setBodySizeLimit(int $bytes)` on your Channel instance. Body sizes exceeding this limit will be truncated,
and delivered to your callback with a `AMQPMessage::$is_truncated` flag set to `true`. The property `AMQPMessage::$body_size` will reflect the true body size of
a received message, which will be higher than `strlen(AMQPMessage::getBody())` if the message has been truncated.
Note that all data above the limit is read from the AMQP Channel and immediately discarded, so there is no way to retrie
没有合适的资源?快使用搜索试试~ 我知道了~
tp6使用rabbitmq
共1035个文件
php:889个
md:32个
json:22个
需积分: 9 0 下载量 46 浏览量
2022-09-22
15:50:45
上传
评论
收藏 1.52MB ZIP 举报
温馨提示
tp6使用rabbitmq
资源详情
资源评论
资源推荐
收起资源包目录
tp6使用rabbitmq (1035个子文件)
random_compat.phar.pubkey.asc 488B
AUTHORS 316B
var-dump-server.bat 142B
openssl.cnf 104B
htmlDescriptor.css 3KB
phpcs.xml.dist 3KB
phpunit.xml.dist 826B
phpunit.xml.dist 559B
.editorconfig 271B
.editorconfig 131B
.env 231B
hiddeninput.exe 9KB
.gitattributes 70B
.gitignore 104B
.gitignore 52B
.gitignore 42B
.gitignore 37B
.gitignore 34B
.gitignore 27B
.gitignore 14B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 6B
.htaccess 218B
.htaccess 13B
.htaccess 0B
nginx.htaccess 0B
nginx.htaccess 0B
favicon.ico 1KB
rabb.iml 288B
htmlDescriptor.js 354B
installed.json 37KB
composer.json 2KB
composer.json 2KB
composer.json 2KB
composer.json 2KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 945B
composer.json 870B
composer.json 848B
composer.json 792B
composer.json 740B
composer.json 720B
composer.json 648B
composer.json 621B
composer.json 562B
composer.json 559B
composer.json 552B
composer.json 513B
LICENSE 24KB
LICENSE 11KB
LICENSE 11KB
LICENSE 11KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
composer.lock 40KB
22.log 320KB
CHANGELOG.md 54KB
README.md 14KB
CONTRIBUTING.md 4KB
History.md 4KB
README.md 3KB
CODE_OF_CONDUCT.md 3KB
README.md 3KB
README.md 3KB
CHANGELOG.md 2KB
README.md 1KB
readme.md 1KB
README.md 1KB
LICENSE.md 1KB
CHANGELOG.md 1KB
README.md 1KB
README.md 919B
README.md 767B
CHANGELOG.md 746B
deprecations.md 724B
README.md 662B
README.md 607B
CHANGELOG.md 585B
README.md 578B
README.md 563B
BACKERS.md 524B
共 1035 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
大得369
- 粉丝: 6486
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 4906-VB一款2个N-Channel沟道SOP8的MOSFET晶体管参数介绍与应用说明
- 4900W-VB一款2个N-Channel沟道SOP8的MOSFET晶体管参数介绍与应用说明
- 4599W-VB一款SOP8封装N+P-Channel场效应MOS管
- 4599-VB一款N+P-Channel沟道SOP8的MOSFET晶体管参数介绍与应用说明
- 4578M-VB一款SOP8封装N+P-Channel场效应MOS管
- 4578GM-VB一款N+P-Channel沟道SOP8的MOSFET晶体管参数介绍与应用说明
- 4578GM-HF-VB一款N+P-Channel沟道SOP8的MOSFET晶体管参数介绍与应用说明
- 4575SS-VB一款SOP8封装N+P-Channel场效应MOS管
- 三菱PLC例程源码生活水泵控制程序
- 4575GM-VB一款SOP8封装N+P-Channel场效应MOS管
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0