3

我正在尝试向 AMQP 发送请求,卡在如何向请求消息中添加标头,下面是我们拥有的包装器

$message = ‘{"empId": ‘.$empId.', “empName”:”my name"}’;
 $resData = $rpcClient->call($message, self::EXCHANGE, self::ROUTING_KEY);

如何在上述消息中添加标题

call 方法是我们编写的包装器

public function call($requestMessage, $exchange, $routingKey)
{
    $this->response = null;
    $this->correlationId = uniqid('abcprod', true);

    $message = new AMQPMessage(
        strval($requestMessage),
        array('correlation_id' => $this->correlationId, 'reply_to' => $this->callbackQueue)
    );

    $this->channel
        ->basic_publish($message, $exchange, $routingKey);

    try {
        $this->channel->wait(null, false, self::REQUEST_TIMEOUT);
    } catch (AMQPTimeoutException $e) {
        return null;
    }

    return $this->response;
}
4

1 回答 1

6

创建消息时,应设置application_headers属性。这应该是一个将标题数组Wire\AMQPTable作为构造函数参数的 a 。

官方amqp_message_headers_snd.php例子:

$message = new AMQPMessage($data);

$headers = new Wire\AMQPTable(array(
   'x-foo'=>'bar',
   'table'=>array('figuf', 'ghf'=>5, 5=>675),
   'num1' => -4294967295,
   'num2' => 5,
   'num3' => -2147483648,
   'true' => true,
   'false' => false,
   'void' => null,
   'date' => new DateTime(),
   'array' => array(null, 'foo', 'bar', 5, 5674625, 'ttt', array(5, 8, 2)),
   'arr_with_tbl' => array(
      'bar',
      5,
      array('foo', 57, 'ee', array('foo'=>'bar', 'baz'=>'boo', 'arr'=>array(1,2,3, true, new DateTime()))),
      67,
      array(
         'foo'=>'bar',
         5=>7,
         8=>'boo',
         'baz'=>3
      )
   ),
   '64bitint' => 9223372036854775807,
   '64bit_uint' => '18446744073709600000',
   '64bitint_neg' => -pow(2, 40)
));
$headers->set('shortshort', -5, Wire\AMQPTable::T_INT_SHORTSHORT);
$headers->set('short', -1024, Wire\AMQPTable::T_INT_SHORT);

$message->set('application_headers', $headers);
于 2017-09-23T11:36:33.327 回答