客户端

composer require php-amqplib/php-amqplib

生产者

send.php 代码

<?php 
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require_once __DIR__ . '/config.php';
$connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['password']);
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);//声明队列


$msg = new AMQPMessage('Hello vilay!');
$channel->basic_publish($msg, '', 'hello');//发布消息到队列

$channel->close(); //通道关闭
$connection->close();//链接关闭

消费者

consumper.php

<?php 
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require_once __DIR__ . '/config.php';
$connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['password']);
$channel = $connection->channel();

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg) {
	echo " [x] Received ", $msg->body, "\n";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while(count($channel->callbacks)) {
		$channel->wait();
}

$channel->close();
$connection->close();

config.php 代码

<?php 

$config = [
		'host' => 'docker.for.mac.host.internal',
		'port' => 5672,
		'user' => 'guest',
		'password' => 'guest'
];

参考博客:RabbitMQ+PHP 教程一(Hello World)