Content · Web Development · PHP

PHP AMQPExchange::publish() Method: Usage and Examples

PHP AMQPExchange::publish() Method: Usage and Examples: Sends a message to an exchange with a routing key for delivery to matching bindings. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

The PHP AMQPExchange::publish API performs a focused operation on an object or value. The examples below show its effect on actual data.

Sends a message to an exchange with a routing key for delivery to matching bindings.

What Is AMQPExchange::publish in PHP?


Sends a message to an exchange with a routing key for delivery to matching bindings.

This API requires a relevant extension or PHP version and cannot be reflected in the local PHP 8.4 runtime. Refer to the version or extension note below before using the examples.

Compatibility note: Install the PECL AMQP extension and configure a RabbitMQ-compatible broker before running this example.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "127.0.0.1",
    "port" => 5672,
    "login" => getenv("AMQP_USERNAME") ?: "guest",
    "password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("events.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$exchange->publish(json_encode(["event" => "created"], JSON_THROW_ON_ERROR), "events.created", AMQP_NOPARAM, ["content_type" => "application/json"]);
$connection->disconnect();

Sends a message to an exchange with a routing key for delivery to matching bindings.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "127.0.0.1",
    "port" => 5672,
    "login" => getenv("AMQP_USERNAME") ?: "guest",
    "password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("audit.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$exchange->publish(json_encode(["event" => "created"], JSON_THROW_ON_ERROR), "events.created", AMQP_NOPARAM, ["content_type" => "application/json"]);
$connection->disconnect();

Publishing successfully does not prove a consumer handled the message.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

$connection = new AMQPConnection([
    "host" => getenv("AMQP_HOST") ?: "127.0.0.1",
    "port" => 5672,
    "login" => getenv("AMQP_USERNAME") ?: "guest",
    "password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("jobs.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$exchange->publish(json_encode(["event" => "created"], JSON_THROW_ON_ERROR), "events.created", AMQP_NOPARAM, ["content_type" => "application/json"]);
$connection->disconnect();

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Publishing successfully does not prove a consumer handled the message.
  • Sends a message to an exchange with a routing key for delivery to matching bindings.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
AMQPExchange::publishSends a message to an exchange with a routing key for delivery to matching bindings.
AMQPQueueExchanges route publications according to bindings; queues retain deliveries until consumers handle them.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of AMQPExchange::publishPublishing successfully does not prove a consumer handled the message.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyExchanges route publications according to bindings; queues retain deliveries until consumers handle them.

Key Takeaways


  • Sends a message to an exchange with a routing key for delivery to matching bindings.
  • Publishing successfully does not prove a consumer handled the message.
  • Use the example that matches the values, runtime version and dependencies in your application.

AMQPExchange::publish is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.