Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Message Queue
    • Quick start
    • Supported tools
      • Python
      • Node.js
      • PHP
      • Celery
      • JMS
      • Laravel
      • Symfony
      • Terraform
      • Golang
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Public materials
  • FAQ

In this article:

  • Installation
  • Getting started
  • Example
  1. Getting started
  2. Code snippets
  3. PHP

Example of using Yandex Message Queue on PHP

Written by
Yandex Cloud
Improved by
Updated at March 28, 2025
  • Installation
  • Getting started
  • Example

With AsyncAws, you can manage Message Queue message queues and send and receive messages.

InstallationInstallation

Install the AsyncAws library:

composer require async-aws/sqs ^1.9

Getting startedGetting started

  1. Create a service account.
  2. Assign the editor role to the service account.
  3. Create a static access key.

Set the environment variables:

export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"

ExampleExample

In this example:

  1. A connection with Message Queue is established.
  2. A message queue is created with the mq_php_sdk_example name.
  3. A message with the text test-message is sent to the queue.
  4. The message is read from the queue and displayed in the terminal.
  5. The message queue is deleted.
<?php

use AsyncAws\Sqs\SqsClient;

require __DIR__ . '/vendor/autoload.php';


$mq = new SqsClient([
    'region' => 'ru-central1',
    'endpoint' => 'https://message-queue.api.cloud.yandex.net',
]);

$result = $mq->createQueue([
    'QueueName' => 'mq_php_sdk_example',
]);

$queueUrl = $result->getQueueUrl();
print('Queue created, URL: ' . $queueUrl . PHP_EOL);

$result = $mq->sendMessage([
    'QueueUrl' => $queueUrl,
    'MessageBody' => 'Test message',
]);

print("Message sent, ID: " . $result->getMessageId() . PHP_EOL);

$result = $mq->receiveMessage([
    'QueueUrl' => $queueUrl,
    'WaitTimeSeconds' => 10,
]);

foreach ($result->getMessages() as $msg) {
    print('Message received:' . PHP_EOL);
    print('ID: ' . $msg->getMessageId() . PHP_EOL);
    print('Body: ' . $msg->getBody() . PHP_EOL);

    $mq->deleteMessage([
        'QueueUrl' => $queueUrl,
        'ReceiptHandle' => $msg->getReceiptHandle(),
    ]);
}

$result = $mq->deleteQueue([
    'QueueUrl' => $queueUrl,
]);

print('Queue deleted' . PHP_EOL);

Was the article helpful?

Previous
Node.js
Next
Celery
Yandex project
© 2025 Yandex.Cloud LLC