在basic_publish()
方法上捕获错误的最佳方式是什么?
<?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('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
?>
答案 0 :(得分:0)
您可以简单地使用try-catch方法
... try { $channel->basic_publish($msg, '', 'hello'); } catch (Error $e) { // do something }