示例 #1 面向对象风格
<?php
$queue = '/queue/foo';
$msg = 'bar';
/* 连接 */
try {
$stomp = new Stomp('tcp://:61613');
} catch(StompException $e) {
die('连接失败: ' . $e->getMessage());
}
/* 发送消息到队列 'foo' */
$stomp->send($queue, $msg);
/* 订阅队列 'foo' 的消息 */
$stomp->subscribe($queue);
/* 读取帧 */
$frame = $stomp->readFrame();
if ($frame->body === $msg) {
var_dump($frame);
/* 确认已收到帧 */
$stomp->ack($frame);
}
/* 关闭连接 */
unset($stomp);
?>以上示例将输出类似以下内容
object(StompFrame)#2 (3) {
["command"]=>
string(7) "MESSAGE"
["headers"]=>
array(5) {
["message-id"]=>
string(41) "ID:php.net-55293-1257226743606-4:2:-1:1:1"
["destination"]=>
string(10) "/queue/foo"
["timestamp"]=>
string(13) "1257226805828"
["expires"]=>
string(1) "0"
["priority"]=>
string(1) "0"
}
["body"]=>
string(3) "bar"
}
示例 #2 过程式风格
<?php
$queue = '/queue/foo';
$msg = 'bar';
/* 连接 */
$link = stomp_connect('ssl://:61612');
/* 检查连接 */
if (!$link) {
die('连接失败: ' . stomp_connect_error());
}
/* 开始事务 */
stomp_begin($link, 't1');
/* 发送消息到队列 'foo' */
stomp_send($link, $queue, $msg, array('transaction' => 't1'));
/* 提交事务 */
stomp_commit($link, 't1');
/* 订阅队列 'foo' 的消息 */
stomp_subscribe($link, $queue);
/* 读取帧 */
$frame = stomp_read_frame($link);
if ($frame['body'] === $msg) {
var_dump($frame);
/* 确认已收到帧 */
stomp_ack($link, $frame['headers']['message-id']);
}
/* 关闭连接 */
stomp_close($link);
?>以上示例将输出类似以下内容
array(3) {
["command"]=>
string(7) "MESSAGE"
["body"]=>
string(3) "bar"
["headers"]=>
array(6) {
["transaction"]=>
string(2) "t1"
["message-id"]=>
string(41) "ID:php.net-55293-1257226743606-4:3:-1:1:1"
["destination"]=>
string(10) "/queue/foo"
["timestamp"]=>
string(13) "1257227037059"
["expires"]=>
string(1) "0"
["priority"]=>
string(1) "0"
}
}