composer安装php-amqplib
配置composer.json
{ "require": { "php-amqplib/php-amqplib": ">=2.6.1" } }安装
composer install
send.php
<?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('channel1', false, false, false, false); $msg = new AMQPMessage('nicce to meet you !'); $channel->basic_publish($msg, '', 'channel1'); echo "信息发送成功!'\n"; $channel->close(); $connection->close();receive.php
<?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('channel1', false, false, false, false); echo "信息接收中.按CTRL+C可退出\n"; $callback = function ($msg) { echo '收到信息:', $msg->body, "\n"; }; $channel->basic_consume('channel1', '', false, true, false, false, $callback); while (count($channel->callbacks)) { $channel->wait(); }效果: