Linux下給PHP安裝amqp擴充套件

MiracleSu的碎片發表於2015-03-21

本文介紹了在Linux下給PHP安裝amqp擴充套件的過程,有需要的朋友可以關注一下。

安裝librabbitmq-c和rabbitmq-codegen

# 下載0-9-1版的rabbitmq-c
git clone git://github.com/alanxz/rabbitmq-c.git
cd rabbitmq-c
# Enable and update the codegen git submodule
git submodule init
git submodule update
# Configure, compile and install
autoreconf -i && ./configure && make && sudo make install

安裝pecl擴充套件

#下載最新的amqp擴充套件
wget http://pecl.php.net/get/amqp-1.0.9.tgz
tar xvzf amqp-1.0.9.tgz
cd amqp-1.0.9 && phpize
./configure --with-amqp && make && sudo make install

記得在php.ini中加入amqp擴充套件:

extension=amqp.so

安裝過程中可能會遇到的問題

1、缺少libtool包

configure.ac: installing ./install-sh
configure.ac: installing ./missing
configure.ac:34: installing ./config.guess
configure.ac:34: installing ./config.sub
Makefile.am:3: Libtool library used but LIBTOOL is undefined
Makefile.am:3:
Makefile.am:3: The usual way to define LIBTOOL is to add AC_PROG_LIBTOOL
Makefile.am:3: to configure.ac and run aclocal and autoconf again.
Makefile.am: C objects in subdir but AM_PROG_CC_C_O not in configure.ac
Makefile.am: installing ./compile
Makefile.am: installing ./depcomp
autoreconf: automake failed with exit status: 1

解決辦法,安裝libtool,ubuntu:

sudo apt-get install libtool

其他系統類似

2、如果還有其他問題,歡迎給我留言,我補上

使用

<?php
//配置資訊
$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'guest',
    'password' => 'guest',
    'vhost'=>'/'
);
//建立連線
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
    die('Not connected :( ' . PHP_EOL);
}
// Open Channel
$channel = new AMQPChannel($conn);
// Declare exchange
$exchange = new AMQPExchange($channel);
$exchange->setName('extest');
$exchange->setType('fanout');
$exchange->declare();
// Create Queue
$queue = new AMQPQueue($channel);
$queue->setName('qutest');
$queue->declare();
// Bind it on the exchange to routing.key
$exchange->bind('qutest', 'routing.key');
$data = array(
    'Name' => 'foobar',
    'Args'  => array("0", "1", "2", "3"),
);
//生產者,向RabbitMQ傳送訊息
$message = $exchange->publish(json_encode($data), 'key');
if (!$message) {
    echo 'Message not sent', PHP_EOL;
} else {
    echo 'Message sent!', PHP_EOL;
}
//消費者
while ($envelope = $queue->get(AMQP_AUTOACK)) {
    echo ($envelope->isRedelivery()) ? 'Redelivery' : 'New Message';
    echo PHP_EOL;
    echo $envelope->getBody(), PHP_EOL;
}
?>

相關文章