您当前的位置:学无止境 > 安装rabbitmq的php扩展和简单使用网站首页学无止境
安装rabbitmq的php扩展和简单使用
发布时间:2024-07-12 14:54:31编辑:三青查看次数:664
php扩展下载地址:https://github.com/php-amqplib/php-amqplib
新建项目目录
cd /home/www/
mkdir rabbitmq
cd rabbitmq/
composer下载PHP扩展
composer require php-amqplib/php-amqplib
新建文件pro.php
pro.php的代码
require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLibConnectionAMQPStreamConnection; use PhpAmqpLibMessageAMQPMessage; $dbName = 'sanqing'; $dbPwd = '111111'; $tableName = 'order'; $connection = new AMQPStreamConnection('localhost', 5672, $dbName, $dbPwd, $tableName); $channel = $connection->channel(); $queue_name = 'hello'; $channel->queue_declare($queue_name, false, true, false, false); $msg = new AMQPMessage('Hello World!',['delivery_mode'=>AMQPMessage::DELIVERY_MODE_PERSISTENT]); $channel->basic_publish($msg, '', $queue_name); $channel->close(); $connection->close();
执行pro.php
php pro.php
新建文件con.php
con.php的代码
<?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLibConnectionAMQPStreamConnection; $dbName = 'sanqing'; $dbPwd = '111111'; $tableName = 'order'; $connection = new AMQPStreamConnection('localhost', 5672, $dbName, $dbPwd, $tableName); $channel = $connection->channel(); $queue_name = 'hello'; $channel->queue_declare($queue_name, false, true, false, false); $callback = function ($msg) { echo ' [x] Received ', $msg->body, "n"; }; $channel->basic_consume($queue_name,'', false, true, false, false, $callback); while($channel->is_open()){ $channel->wait(); } $channel->close(); $connection->close();
执行con.php
php con.php
关键字词:rabbitmq,php,扩展
评论: