PHPackages                             baselsoftwaredev/azure-service-bus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. baselsoftwaredev/azure-service-bus

ActiveLibrary

baselsoftwaredev/azure-service-bus
==================================

This project provides a library that make it easy to access Microsoft service bus (queues and topics).

02PHP

Since Aug 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/baselsoftwaredev/azure-service-bus)[ Packagist](https://packagist.org/packages/baselsoftwaredev/azure-service-bus)[ RSS](/packages/baselsoftwaredev-azure-service-bus/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (3)Used By (0)

Azure Service Bus for PHP
=========================

[](#azure-service-bus-for-php)

This project provides a library that make it easy to access Microsoft service bus (queues and topics).

Most code has been lifted from the now archived azure for php sdk library.

Old previous non-relevant code has been removed

The old library was build on PHP 5.6, therefore it has been upgraded to support at least the latest LTS PHP 7.4.

Features
========

[](#features)

- Queues: create, list and delete queues; send, receive, unlock and delete messages
- Topics: create, list, and delete topics; create, list, and delete subscriptions; send, receive, unlock and delete messages; create, list, and delete rules

Getting Started
===============

[](#getting-started)

Download Source Code
--------------------

[](#download-source-code)

To get the source code from GitHub, type

```
git clone https://github.com/baselsoftwaredev/azure-service-bus.git
cd ./azure-service-bus

```

Install via Composer
--------------------

[](#install-via-composer)

Planning to add composer package registry.

Usage
=====

[](#usage)

Getting Started
---------------

[](#getting-started-1)

There are four basic steps that have to be performed before you can make a call to any Microsoft Azure API when using the libraries.

- First, include the autoloader script:

    ```
    require_once "vendor/autoload.php";
    ```
- Include the namespaces you are going to use.

    To create any Microsoft Azure service client you need to use the **ServicesBuilder** class:

    ```
    use WindowsAzure\Common\ServicesBuilder;
    ```

    To process exceptions you need:

    ```
    use WindowsAzure\Common\ServiceException;
    ```
- To instantiate the service client you will also need a valid connection string. For accessing the service bus the format is:

    ```
    Endpoint=[yourEndpoint];SharedSecretIssuer=[yourWrapAuthenticationName];SharedSecretValue=[yourWrapPassword]

    ```

    Where the Endpoint is typically of the format `https://[yourNamespace].servicebus.windows.net`.
- Instantiate a for Service Bus "REST Proxy" - a wrapper around the available calls for the given service.

    ```
    $serviceBusRestProxy = ServicesBuilder::getInstance()->createServiceBusService($connectionString);
    ```

### Create a queue

[](#create-a-queue)

A **QueueRestProxy** object lets you create a queue with the **createQueue** method. When creating a queue, you can set options on the queue, but doing so is not required.

```
$createQueueOptions = new CreateQueueOptions();
$createQueueOptions->addMetaData("key1", "value1");
$createQueueOptions->addMetaData("key2", "value2");

try {
  // Create queue.
  $queueRestProxy->createQueue("myqueue", $createQueueOptions);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

[Error Codes and Messages for Queues](http://msdn.microsoft.com/en-us/library/windowsazure/dd179446.aspx)

### Add a message to a queue

[](#add-a-message-to-a-queue)

To add a message to a queue, use **QueueRestProxy-&gt;createMessage**. The method takes the queue name, the message text, and message options (which are optional). For compatibility with others you may need to base64 encode message.

```
try {
  // Create message.
  $msg = "Hello World!";
  // optional: $msg = base64_encode($msg);
  $queueRestProxy->createMessage("myqueue", $msg);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

### Peek at the next message

[](#peek-at-the-next-message)

You can peek at a message (or messages) at the front of a queue without removing it from the queue by calling **QueueRestProxy-&gt;peekMessages**.

```
// OPTIONAL: Set peek message options.
$message_options = new PeekMessagesOptions();
$message_options->setNumberOfMessages(1); // Default value is 1.

try {
  $peekMessagesResult = $queueRestProxy->peekMessages("myqueue", $message_options);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}

$messages = $peekMessagesResult->getQueueMessages();

// View messages.
$messageCount = count($messages);
if($messageCount getMessageId()."";
    echo "Date: ".date_format($message->getInsertionDate(), 'Y-m-d')."";
    echo "Message text: ".$message->getMessageText()."";
  }
}
```

### De-queue the next message

[](#de-queue-the-next-message)

Your code removes a message from a queue in two steps. First, you call **QueueRestProxy-&gt;listMessages**, which makes the message invisible to any other code reading from the queue. By default, this message will stay invisible for 30 seconds (if the message is not deleted in this time period, it will become visible on the queue again). To finish removing the message from the queue, you must call **QueueRestProxy-&gt;deleteMessage**.

```
// Get message.
$listMessagesResult = $queueRestProxy->listMessages("myqueue");
$messages = $listMessagesResult->getQueueMessages();
$message = $messages[0];

// Process message

// Get message Id and pop receipt.
$messageId = $message->getMessageId();
$popReceipt = $message->getPopReceipt();

try {
  // Delete message.
  $queueRestProxy->deleteMessage("myqueue", $messageId, $popReceipt);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

Service Bus Queues
------------------

[](#service-bus-queues)

The current PHP Service Bus APIs only support ACS connection strings. You need to use PowerShell to create a new ACS Service Bus namespace at the present time. First, make sure you have Azure PowerShell installed, then in a PowerShell command prompt, run

```
Add-AzureAccount # this will sign you in
New-AzureSBNamespace -CreateACSNamespace $true -Name 'mytestbusname' -Location 'West US' -NamespaceType 'Messaging'
```

If it is sucessful, you will get the connection string in the PowerShell output. If you get connection errors with it and the conection string looks like Endpoint=sb://..., change it to **Endpoint=https://...**

### Create a Queue

[](#create-a-queue-1)

```
try {
  $queueInfo = new QueueInfo("myqueue");

  // Create queue.
  $serviceBusRestProxy->createQueue($queueInfo);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

[Error Codes and Messages](http://msdn.microsoft.com/en-us/library/windowsazure/dd179357)

### Send a Message

[](#send-a-message)

To send a message to a Service Bus queue, your application will call the **ServiceBusRestProxy-&gt;sendQueueMessage** method. Messages sent to (and received from ) Service Bus queues are instances of the **BrokeredMessage** class.

```
try {
  // Create message.
  $message = new BrokeredMessage();
  $message->setBody("my message");

  // Send message.
  $serviceBusRestProxy->sendQueueMessage("myqueue", $message);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

### Receive a Message

[](#receive-a-message)

The primary way to receive messages from a queue is to use a **ServiceBusRestProxy-&gt;receiveQueueMessage** method. Messages can be received in two different modes: **ReceiveAndDelete** (mark message as consumed on read) and **PeekLock** (locks message for a period of time, but does not delete).

The example below demonstrates how a message can be received and processed using **PeekLock** mode (not the default mode).

```
try {
  // Set the receive mode to PeekLock (default is ReceiveAndDelete).
  $options = new ReceiveMessageOptions();
  $options->setPeekLock(true);

  // Receive message.
  $message = $serviceBusRestProxy->receiveQueueMessage("myqueue", $options);
  echo "Body: ".$message->getBody()."";
  echo "MessageID: ".$message->getMessageId()."";

  // *** Process message here ***

  // Delete message.
  $serviceBusRestProxy->deleteMessage($message);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

Service Bus Topics
------------------

[](#service-bus-topics)

### Create a Topic

[](#create-a-topic)

```
try {
  // Create topic.
  $topicInfo = new TopicInfo("mytopic");
  $serviceBusRestProxy->createTopic($topicInfo);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

### Create a subscription with the default (MatchAll) filter

[](#create-a-subscription-with-the-default-matchall-filter)

```
try {
  // Create subscription.
  $subscriptionInfo = new SubscriptionInfo("mysubscription");
  $serviceBusRestProxy->createSubscription("mytopic", $subscriptionInfo);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

### Send a message to a topic

[](#send-a-message-to-a-topic)

Messages sent to Service Bus topics are instances of the **BrokeredMessage** class.

```
try {
  // Create message.
  $message = new BrokeredMessage();
  $message->setBody("my message");

  // Send message.
  $serviceBusRestProxy->sendTopicMessage("mytopic", $message);
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

### Receive a message from a topic

[](#receive-a-message-from-a-topic)

The primary way to receive messages from a subscription is to use a **ServiceBusRestProxy-&gt;receiveSubscriptionMessage** method. Received messages can work in two different modes: **ReceiveAndDelete** (the default) and **PeekLock** similarly to Service Bus Queues.

The example below demonstrates how a message can be received and processed using **ReceiveAndDelete** mode (the default mode).

```
try {
  // Set receive mode to PeekLock (default is ReceiveAndDelete)
  $options = new ReceiveMessageOptions();
  $options->setReceiveAndDelete();

  // Get message.
  $message = $serviceBusRestProxy->receiveSubscriptionMessage("mytopic",
                                "mysubscription",
                                $options);
  echo "Body: ".$message->getBody()."";
  echo "MessageID: ".$message->getMessageId()."";
} catch(ServiceException $e){
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."";
}
```

Need Help?
==========

[](#need-help)

Raise an issue.

Contribute Code or Provide Feedback
===================================

[](#contribute-code-or-provide-feedback)

Information in setting up for development can be found at [develop.md](doc/develop.md)

If you encounter any bugs with the library please file an issue in the [Issues](https://github.com/baselsoftwaredev//issues) section of the project.

Learn More
==========

[](#learn-more)

[Microsoft Azure Service Bus](https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor3

3 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12848116?v=4)[Basel](/maintainers/baselsoftwaredev)[@baselsoftwaredev](https://github.com/baselsoftwaredev)

---

Top Contributors

[![ogail](https://avatars.githubusercontent.com/u/1034045?v=4)](https://github.com/ogail "ogail (172 commits)")[![jcookems](https://avatars.githubusercontent.com/u/1087451?v=4)](https://github.com/jcookems "jcookems (159 commits)")[![VladimirTsyshnatiy](https://avatars.githubusercontent.com/u/5425791?v=4)](https://github.com/VladimirTsyshnatiy "VladimirTsyshnatiy (118 commits)")[![rnrneverdies](https://avatars.githubusercontent.com/u/2011474?v=4)](https://github.com/rnrneverdies "rnrneverdies (52 commits)")[![sergey-shandar](https://avatars.githubusercontent.com/u/902339?v=4)](https://github.com/sergey-shandar "sergey-shandar (44 commits)")[![yaqiyang](https://avatars.githubusercontent.com/u/14302929?v=4)](https://github.com/yaqiyang "yaqiyang (44 commits)")[![baselsoftwaredev](https://avatars.githubusercontent.com/u/12848116?v=4)](https://github.com/baselsoftwaredev "baselsoftwaredev (32 commits)")[![devigned](https://avatars.githubusercontent.com/u/386473?v=4)](https://github.com/devigned "devigned (8 commits)")[![SergeyRazmyslov](https://avatars.githubusercontent.com/u/7600904?v=4)](https://github.com/SergeyRazmyslov "SergeyRazmyslov (6 commits)")[![rdohms](https://avatars.githubusercontent.com/u/94331?v=4)](https://github.com/rdohms "rdohms (6 commits)")[![jeffwilcox](https://avatars.githubusercontent.com/u/427913?v=4)](https://github.com/jeffwilcox "jeffwilcox (5 commits)")[![phansys](https://avatars.githubusercontent.com/u/1231441?v=4)](https://github.com/phansys "phansys (5 commits)")[![Sazpaimon](https://avatars.githubusercontent.com/u/247570?v=4)](https://github.com/Sazpaimon "Sazpaimon (4 commits)")[![antonba](https://avatars.githubusercontent.com/u/1065579?v=4)](https://github.com/antonba "antonba (4 commits)")[![katmsft](https://avatars.githubusercontent.com/u/21050104?v=4)](https://github.com/katmsft "katmsft (3 commits)")[![bradygaster](https://avatars.githubusercontent.com/u/41929050?v=4)](https://github.com/bradygaster "bradygaster (3 commits)")[![XiaoningLiu](https://avatars.githubusercontent.com/u/6889160?v=4)](https://github.com/XiaoningLiu "XiaoningLiu (2 commits)")[![azure-sdk](https://avatars.githubusercontent.com/u/53356347?v=4)](https://github.com/azure-sdk "azure-sdk (2 commits)")[![YuzorMa](https://avatars.githubusercontent.com/u/11199246?v=4)](https://github.com/YuzorMa "YuzorMa (1 commits)")[![captn3m0](https://avatars.githubusercontent.com/u/584253?v=4)](https://github.com/captn3m0 "captn3m0 (1 commits)")

### Embed Badge

![Health badge](/badges/baselsoftwaredev-azure-service-bus/health.svg)

```
[![Health](https://phpackages.com/badges/baselsoftwaredev-azure-service-bus/health.svg)](https://phpackages.com/packages/baselsoftwaredev-azure-service-bus)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
