$ sudo yum install php-pecl-amqp
setting.php
$address = 'amqp://guest:guest@192.168.33.10'; $outbox = 'outbox_test'; $resultbox = 'results_test'; $recipientDomain = 'gmail.com'; $recipientEmail = 'hogehoge@gmail.com'; $fromAddress = 'info@hpscript.com';
send.php
require_once('settings.php');
try {
$connection = new AMQPConnection(array(
'host' = $hostname,
'login' => $username,
'password' => $password
));
$connection->connect();
}
catch(AMQPException $exception){
echo "Could not establish a connection to the RabbitMQ server.\n";
}
try {
$channel = new AMQPChannel($connection);
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost(creating channel).\n";
}
try {
$exchange = new AMQPExchange($channel);
$exchange->setName('exchange1');
$exchange->setType('fanout');
$exchange->declareExchange();
} catch (AMQPExchangeException $exception){
echo "CHannel is not connected to broker(declaring exchange).\n";
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost(declaring exchange).\n";
}
try {
$queue = new AMQPQueue($channel);
$queue->setName($outbox);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
$queue->bind('exchange1','key2');
} catch (AMQPQueueException $exception){
echo "Channel is not connected to a broker(creating queue).\n";
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost.(creating queue)/\n";
}
$jsonMessage = json_encode(array(
'envelope'=>$fromAddress,
'recipient' => $recipientEmail,
'mime' => "From:".$fromAddress."\r\n"
. "To:". $recipientEmail . "\r\n"
. "Subject: Example subject\r\n\r\n"
. "This is the example message text"
));
try {
$message = $exchange->publish($jsonMessage,'key2');
} catch (AMQPExchangeException $exception){
echo "Channel is not connected to a broker(publishing message on queue).\n";
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost(publishing message on queue).\n";
} catch (AMQPChannelException $exception){
echo "The channel is not open(publishing message on queue).\n";
}
$connection->disconnect();
require_once('settings.php');
try {
$connection = new AMQPConnection(array(
'host' => $hostname,
'login' => $username,
'password' => $password,
'vhost' => $vhost
));
$connection->connect();
} catch (AMQPException $exception){
echo "Could not establish a connection to the RabbitMQ servier.\n";
}
try {
$channel = new AMQPChannel($connection);
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost(creating channel).\n";
}
try {
$queue = new AMQPQueue($channel);
$queue->setName($resultbox);
$queue->setFlags(AMQP_DURABLE);
$queue->bind('exchange1','key2');
$queue->declareQueue();
} catch(AMQPQueueException $exception){
echo "Channel is not connected to a broker(creating queue).\n";
} catch (AMQPConnectionException $exception){
echo "Connection to the broker was lost.(creating queue)/\n";
}
while ($envelope = $queue->get()){
echo "Received:\n";
echo $envelope->getBody(). "\n";
echo "----------------------------------------------\n\n";
}
$connection->disconnect();
$ php result.php
PHP Fatal error: Uncaught Error: Class ‘AMQPConnection’ not found in /home/vagrant/dev/mailer/result.php:6
Stack trace:
#0 {main}
thrown in /home/vagrant/dev/mailer/result.php on line 6
ん? どういうこと?
RabbitMQの使い方がよくわかってないな。。