{"id":13592024,"url":"https://github.com/php-amqplib/php-amqplib","last_synced_at":"2025-05-13T11:06:17.423Z","repository":{"id":1859369,"uuid":"2784293","full_name":"php-amqplib/php-amqplib","owner":"php-amqplib","description":"The most widely used PHP client for RabbitMQ","archived":false,"fork":false,"pushed_at":"2025-03-10T21:01:47.000Z","size":9079,"stargazers_count":4530,"open_issues_count":16,"forks_count":1033,"subscribers_count":152,"default_branch":"master","last_synced_at":"2025-05-13T11:05:59.028Z","etag":null,"topics":["amqp","hacktoberfest","messaging","php","php-amqplib","rabbitmq"],"latest_commit_sha":null,"homepage":"http://www.rabbitmq.com/getstarted.html","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"hpneo/gmaps","license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-amqplib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2011-11-15T23:49:33.000Z","updated_at":"2025-05-12T14:56:19.000Z","dependencies_parsed_at":"2023-07-05T19:33:40.526Z","dependency_job_id":"a077ff21-83a7-42a6-87fc-52233f976803","html_url":"https://github.com/php-amqplib/php-amqplib","commit_stats":{"total_commits":898,"total_committers":217,"mean_commits":4.138248847926267,"dds":0.8006681514476615,"last_synced_commit":"0595d99f12b664b8fc9a5ab02f4f38dc8468fe01"},"previous_names":["videlalvaro/php-amqplib"],"tags_count":81,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-amqplib%2Fphp-amqplib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-amqplib%2Fphp-amqplib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-amqplib%2Fphp-amqplib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-amqplib%2Fphp-amqplib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-amqplib","download_url":"https://codeload.github.com/php-amqplib/php-amqplib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253929362,"owners_count":21985802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amqp","hacktoberfest","messaging","php","php-amqplib","rabbitmq"],"created_at":"2024-08-01T16:01:04.953Z","updated_at":"2025-05-13T11:06:17.396Z","avatar_url":"https://github.com/php-amqplib.png","language":"PHP","readme":"# php-amqplib #\n\n![PHPUnit tests](https://github.com/php-amqplib/php-amqplib/workflows/PHPUnit%20tests/badge.svg)\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![Software License][ico-license]](LICENSE)\n\n[![codecov](https://codecov.io/gh/php-amqplib/php-amqplib/branch/master/graph/badge.svg?token=tgeYkUsaDM)](https://codecov.io/gh/php-amqplib/php-amqplib)\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n\nThis library is a _pure PHP_ implementation of the [AMQP 0-9-1 protocol](http://www.rabbitmq.com/tutorials/amqp-concepts.html).\nIt's been tested against [RabbitMQ](http://www.rabbitmq.com/).\n\nThe 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).\n\nPlease 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.\n\n## Project Maintainers\n\nThanks to [videlalvaro](https://github.com/videlalvaro) and [postalservice14](https://github.com/postalservice14) for creating `php-amqplib`.\n\nThe 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.\n\n## Supported RabbitMQ Versions ##\n\nStarting 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).\nUsually server upgrades do not require any application code changes since\nthe protocol changes very infrequently but please conduct your own testing before upgrading.\n\n## Supported RabbitMQ Extensions ##\n\nSince the library uses `AMQP 0.9.1` we added support for the following RabbitMQ extensions:\n\n* Exchange to Exchange Bindings\n* Basic Nack\n* Publisher Confirms\n* Consumer Cancel Notify\n\nExtensions that modify existing methods like `alternate exchanges` are also supported.\n\n### Related libraries\n\n* [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.\n\n* [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.\n\n## Setup ##\n\nEnsure you have [composer](http://getcomposer.org) installed, then run the following command:\n\n```bash\ncomposer require php-amqplib/php-amqplib\n```\n\nThat will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your\n.php files in order to use the library\n\n```php\nrequire_once __DIR__.'/vendor/autoload.php';\n```\n\nThen you need to `use` the relevant classes, for example:\n\n```php\nuse PhpAmqpLib\\Connection\\AMQPStreamConnection;\nuse PhpAmqpLib\\Message\\AMQPMessage;\n```\n\n## Usage ##\n\nWith RabbitMQ running open two Terminals and on the first one execute the following commands to start the consumer:\n\n```bash\ncd php-amqplib/demo\nphp amqp_consumer.php\n```\n\nThen on the other Terminal do:\n\n```bash\ncd php-amqplib/demo\nphp amqp_publisher.php some text to publish\n```\n\nYou should see the message arriving to the process on the other Terminal\n\nThen to stop the consumer, send to it the `quit` message:\n\n```bash\nphp amqp_publisher.php quit\n```\n\nIf you need to listen to the sockets used to connect to RabbitMQ then see the example in the non blocking consumer.\n\n```bash\nphp amqp_consumer_non_blocking.php\n```\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## API Documentation ##\n\nhttp://php-amqplib.github.io/php-amqplib/\n\n## Tutorials ##\n\nTo not repeat ourselves, if you want to learn more about this library,\nplease refer to the [official RabbitMQ tutorials](http://www.rabbitmq.com/tutorials/tutorial-one-php.html).\n\n## More Examples ##\n\n- `amqp_ha_consumer.php`: demos the use of mirrored queues.\n- `amqp_consumer_exclusive.php` and `amqp_publisher_exclusive.php`: demos fanout exchanges using exclusive queues.\n- `amqp_consumer_fanout_{1,2}.php` and `amqp_publisher_fanout.php`: demos fanout exchanges with named queues.\n- `amqp_consumer_pcntl_heartbeat.php`: demos signal-based heartbeat sender usage.\n- `basic_get.php`: demos obtaining messages from the queues by using the _basic get_ AMQP call.\n\n## Multiple hosts connections ##\n\nIf you have a cluster of multiple nodes to which your application can connect,\nyou can start a connection with an array of hosts. To do that you should use\nthe `create_connection` static method.\n\nFor example:\n```php\n$connection = AMQPStreamConnection::create_connection([\n    ['host' =\u003e HOST1, 'port' =\u003e PORT, 'user' =\u003e USER, 'password' =\u003e PASS, 'vhost' =\u003e VHOST],\n    ['host' =\u003e HOST2, 'port' =\u003e PORT, 'user' =\u003e USER, 'password' =\u003e PASS, 'vhost' =\u003e VHOST]\n],\n$options);\n```\n\nThis code will try to connect to `HOST1` first, and connect to `HOST2` if the\nfirst connection fails. The method returns a connection object for the first\nsuccessful connection. Should all connections fail it will throw the exception\nfrom the last connection attempt.\n\nSee `demo/amqp_connect_multiple_hosts.php` for more examples.\n\n## Batch Publishing ##\n\nLet'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`.\nThen you could make use of the `batch_basic_publish` library feature. You can batch messages like this:\n\n```php\n$msg = new AMQPMessage($msg_body);\n$ch-\u003ebatch_basic_publish($msg, $exchange);\n\n$msg2 = new AMQPMessage($msg_body);\n$ch-\u003ebatch_basic_publish($msg2, $exchange);\n```\n\nand then send the batch like this:\n\n```php\n$ch-\u003epublish_batch();\n```\n\n### When do we publish the message batch? ###\n\nLet'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.\nYou could send it every 50 messages, or every hundred. That's up to you.\n\n## Optimized Message Publishing ##\n\nAnother way to speed up your message publishing is by reusing the `AMQPMessage` message instances. You can create your new message like this:\n\n```php\n$properties = array('content_type' =\u003e 'text/plain', 'delivery_mode' =\u003e AMQPMessage::DELIVERY_MODE_PERSISTENT);\n$msg = new AMQPMessage($body, $properties);\n$ch-\u003ebasic_publish($msg, $exchange);\n```\n\nNow 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:\n\n```php\n$msg-\u003esetBody($body2);\n$ch-\u003ebasic_publish($msg, $exchange);\n```\n\n## Truncating Large Messages ##\n\nAMQP 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\nwithin the library before the callback passed to `basic_consume` is called.\n\nTo avoid this, you can call the method `AMQPChannel::setBodySizeLimit(int $bytes)` on your Channel instance. Body sizes exceeding this limit will be truncated,\nand 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\na received message, which will be higher than `strlen(AMQPMessage::getBody())` if the message has been truncated.\n\nNote that all data above the limit is read from the AMQP Channel and immediately discarded, so there is no way to retrieve it within your\ncallback. If you have another consumer which can handle messages with larger payloads, you can use `basic_reject` or `basic_nack` to tell\nthe server (which still has a complete copy) to forward it to a Dead Letter Exchange.\n\nBy default, no truncation will occur. To disable truncation on a Channel that has had it enabled, pass `0` (or `null`) to `AMQPChannel::setBodySizeLimit()`.\n\n## Connection recovery ##\n\nSome RabbitMQ clients using automated connection recovery mechanisms to reconnect\nand recover channels and consumers in case of network errors.\n\nSince this client is using a single-thread, you can set up connection recovery\nusing exception handling mechanism.\n\nExceptions which might be thrown in case of connection errors:\n\n```php\nPhpAmqpLib\\Exception\\AMQPConnectionClosedException\nPhpAmqpLib\\Exception\\AMQPIOException\n\\RuntimeException\n\\ErrorException\n```\n\nSome other exceptions might be thrown, but connection can still be there. It's\nalways a good idea to clean up an old connection when handling an exception\nbefore reconnecting.\n\nFor example, if you want to set up a recovering connection:\n\n```php\n$connection = null;\n$channel = null;\nwhile(true){\n    try {\n        $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);\n        // Your application code goes here.\n        do_something_with_connection($connection);\n    } catch(AMQPRuntimeException $e) {\n        echo $e-\u003egetMessage();\n        cleanup_connection($connection);\n        usleep(WAIT_BEFORE_RECONNECT_uS);\n    } catch(\\RuntimeException $e) {\n        cleanup_connection($connection);\n        usleep(WAIT_BEFORE_RECONNECT_uS);\n    } catch(\\ErrorException $e) {\n        cleanup_connection($connection);\n        usleep(WAIT_BEFORE_RECONNECT_uS);\n    }\n}\n\n```\n\nA full example is in `demo/connection_recovery_consume.php`.\n\nThis code will reconnect and retry the application code every time the\nexception occurs. Some exceptions can still be thrown and should not be handled\nas a part of reconnection process, because they might be application errors.\n\nThis approach makes sense mostly for consumer applications, producers will\nrequire some additional application code to avoid publishing the same message\nmultiple times.\n\nThis was a simplest example, in a real-life application you might want to\ncontrol retr count and maybe gracefully degrade wait time to reconnection.\n\nYou can find a more excessive example in [#444](https://github.com/php-amqplib/php-amqplib/issues/444)\n\n\n## UNIX Signals ##\n\nIf you have installed [PCNTL extension](http://www.php.net/manual/en/book.pcntl.php) dispatching of signal will be handled when consumer is not processing message.\n\n```php\n$pcntlHandler = function ($signal) {\n    switch ($signal) {\n        case \\SIGTERM:\n        case \\SIGUSR1:\n        case \\SIGINT:\n            // some stuff before stop consumer e.g. delete lock etc\n            pcntl_signal($signal, SIG_DFL); // restore handler\n            posix_kill(posix_getpid(), $signal); // kill self with signal, see https://www.cons.org/cracauer/sigint.html\n        case \\SIGHUP:\n            // some stuff to restart consumer\n            break;\n        default:\n            // do nothing\n    }\n};\n\npcntl_signal(\\SIGTERM, $pcntlHandler);\npcntl_signal(\\SIGINT,  $pcntlHandler);\npcntl_signal(\\SIGUSR1, $pcntlHandler);\npcntl_signal(\\SIGHUP,  $pcntlHandler);\n```\n\nTo disable this feature just define constant `AMQP_WITHOUT_SIGNALS` as `true`\n\n```php\n\u003c?php\ndefine('AMQP_WITHOUT_SIGNALS', true);\n\n... more code\n\n```\n\n\n## Signal-based Heartbeat ##\n\nIf you have installed [PCNTL extension](http://www.php.net/manual/en/book.pcntl.php) and are using PHP 7.1 or greater,\nyou can register a signal-based heartbeat sender.\n\n```php\n\u003c?php\n\n$sender = new PCNTLHeartbeatSender($connection);\n$sender-\u003eregister();\n... code\n$sender-\u003eunregister();\n\n```\n\n## Debugging ##\n\nIf you want to know what's going on at a protocol level then add the following constant to your code:\n\n```php\n\u003c?php\ndefine('AMQP_DEBUG', true);\n\n... more code\n\n?\u003e\n```\n\n## Benchmarks ##\n\nTo run the publishing/consume benchmark type:\n\n```bash\nmake benchmark\n```\n\n## Tests and Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Using AMQP 0.8 ##\n\nIf you still want to use the old version of the protocol then you can do it by setting the following constant in your configuration code:\n\n```php\ndefine('AMQP_PROTOCOL', '0.8');\n```\n\nThe default value is `'0.9.1'`.\n\n## Providing your own autoloader ##\n\nIf for some reason you don't want to use composer, then you need to have an autoloader in place fo the library classes. People have [reported](https://github.com/videlalvaro/php-amqplib/issues/61#issuecomment-37855050) to use this [autoloader](https://gist.github.com/jwage/221634) with success.\n\n## Original README: ##\n\nBelow is the original README file content. Credits goes to the original authors.\n\nPHP library implementing Advanced Message Queuing Protocol (AMQP).\n\nThe library is port of python code of py-amqplib\nhttp://barryp.org/software/py-amqplib/\n\nIt have been tested with RabbitMQ server.\n\nProject home page: http://code.google.com/p/php-amqplib/\n\nFor discussion, please join the group:\n\nhttp://groups.google.com/group/php-amqplib-devel\n\nFor bug reports, please use bug tracking system at the project page.\n\nPatches are very welcome!\n\nAuthor: Vadim Zaliva \u003clord@crocodile.org\u003e\n\n[ico-version]: https://img.shields.io/packagist/v/php-amqplib/php-amqplib.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-LGPL_2.1-brightgreen.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/php-amqplib/php-amqplib.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/php-amqplib/php-amqplib.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/php-amqplib/php-amqplib.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/php-amqplib/php-amqplib\n[link-scrutinizer]: https://scrutinizer-ci.com/g/php-amqplib/php-amqplib/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/php-amqplib/php-amqplib\n[link-downloads]: https://packagist.org/packages/php-amqplib/php-amqplib\n[link-author]: https://github.com/php-amqplib\n[link-contributors]: ../../contributors\n","funding_links":[],"categories":["PHP","目录","Table of Contents","队列 Queue","Queues \u0026 Event Systems","队列( Queue )"],"sub_categories":["队列 Queue","Queue"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-amqplib%2Fphp-amqplib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-amqplib%2Fphp-amqplib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-amqplib%2Fphp-amqplib/lists"}