Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
Yandex Message Queue
  • Getting started
    • Quick start
    • Supported tools
    • Code samples
      • Node.js
      • PHP
      • JMS
      • Laravel
      • Terraform
  • Step-by-step instructions
    • Overview
    • Creating a new message queue
    • Sending messages
    • Receiving and deleting messages
    • Deleting a message queue
    • Monitoring processes in queues
  • Concepts
    • Overview
    • Message queues
    • Messages
    • Deduplication
    • Visibility timeout
    • Long Polling
    • Dead Letter Queue
    • Delay queues
    • Quotas and limits
  • Practical guidelines
    • Converting a video to a GIF in Python
  • Access management
  • Pricing policy
  • Yandex Message Queue API
    • Overview
    • Queue
      • CreateQueue
      • DeleteQueue
      • GetQueueAttributes
      • GetQueueUrl
      • ListQueues
      • PurgeQueue
      • SetQueueAttributes
    • Message
      • ChangeMessageVisibility
      • ChangeMessageVisibilityBatch
      • DeleteMessage
      • DeleteMessageBatch
      • ReceiveMessage
      • SendMessage
      • SendMessageBatch
    • Data types
      • BatchResultErrorEntry
      • ChangeMessageVisibilityBatchRequestEntry
      • ChangeMessageVisibilityBatchResultEntry
      • DeleteMessageBatchRequestEntry
      • DeleteMessageBatchResultEntry
      • Message
      • MessageAttributeValue
      • SendMessageBatchRequestEntry
      • SendMessageBatchResultEntry
    • Common errors
  • Questions and answers
  1. Getting started
  2. Code samples
  3. PHP

Example of using Yandex Message Queue on PHP

Written by
Yandex Cloud
,
improved by
  • Installation
  • Before you start
  • Example

Using the AWS SDK for PHP, you can manage Message Queue message queues and send and receive messages.

Installation

Install the AWS SDK for PHP by following the instructions on the official site.

$ composer require aws/aws-sdk-php-resources

Before you start

  1. Create a service account.
  2. Assign a role to a editor 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>"

Example

In this example:

  1. A connection with Message Queue is established.
  2. A message queue is created with the name ymq_php_sdk_example.
  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

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

use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;

$ymq = new Aws\Sqs\SqsClient([
    'version' => 'latest',
    'region' => 'ru-central1',
    'endpoint' => 'https://message-queue.api.cloud.yandex.net',
]);

$result = $ymq->createQueue([
    'QueueName' => 'ymq_php_sdk_example',
]);

$queueUrl = $result["QueueUrl"];
print('Queue created, URL: ' . $queueUrl . PHP_EOL);

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

print("Message sent, ID: " . $result["MessageId"] . PHP_EOL);

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

foreach ($result["Messages"] as $msg) {
    print('Message received:' . PHP_EOL);
    print('ID: ' . $msg['MessageId'] . PHP_EOL);
    print('Body: ' . $msg['Body'] . PHP_EOL);

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

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

print('Queue deleted' . PHP_EOL);

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Installation
  • Before you start
  • Example