【问题标题】:PHP AMQP pass headers in requestPHP AMQP 在请求中传递标头
【发布时间】:2015-07-09 10:18:17
【问题描述】:

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

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

如何在上面的消息中添加标题

调用方法就是我们写的包装器

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;
}

【问题讨论】:

    标签: php amqp php-amqplib


    【解决方案1】:

    创建消息时,应设置application_headers 属性。 这应该是一个 Wire\AMQPTable,它以一个 array 的标头作为构造函数参数。

    官方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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多