9951 explained code solutions for 126 technologies


php-awsHow can I use an AWS SQS Worker with PHP?


Using an AWS SQS Worker with PHP requires the use of the AWS SDK for PHP. The SDK provides access to the Amazon SQS service and allows you to perform operations such as creating, sending, and receiving messages.

The following example code demonstrates how to use the SDK to create a worker that will poll an SQS queue for messages and process them.

<?php
// Include the AWS SDK for PHP
require 'vendor/autoload.php';

// Create an instance of the SQS client
$sqsClient = new Aws\Sqs\SqsClient([
    'region'  => 'us-east-1',
    'version' => '2012-11-05'
]);

// Set the queue URL
$queueUrl = 'https://sqs.us-east-1.amazonaws.com/123456789012/my-queue';

// Start the worker loop
while(true) {
    // Receive a message from the queue
    $result = $sqsClient->receiveMessage([
        'QueueUrl' => $queueUrl,
        'MaxNumberOfMessages' => 1
    ]);

    // Check if a message was received
    if(count($result->get('Messages')) > 0) {
        // Get the message body
        $messageBody = $result->get('Messages')[0]['Body'];

        // Do something with the message
        // ...

        // Delete the message from the queue
        $sqsClient->deleteMessage([
            'QueueUrl' => $queueUrl,
            'ReceiptHandle' => $result->get('Messages')[0]['ReceiptHandle']
        ]);
    }
}

The code above will create a loop that will continuously poll the SQS queue for messages. When a message is received, it will be processed and then deleted from the queue.

Code explanation

  1. Include the AWS SDK for PHP: This will allow us to access the SQS service.
  2. Create an instance of the SQS client: We will use this to perform operations on the SQS queue.
  3. Set the queue URL: This will be the URL of the SQS queue we want to poll.
  4. Start the worker loop: This will continuously poll the queue for messages.
  5. Receive a message from the queue: This will retrieve the next message from the queue.
  6. Check if a message was received: If a message was retrieved, we will process it.
  7. Get the message body: This will get the body of the message from the result.
  8. Do something with the message: This is where we will process the message.
  9. Delete the message from the queue: This will delete the message from the queue after it has been processed.

For more information, please see the AWS SDK for PHP documentation and the Amazon SQS Developer Guide.

Edit this code on GitHub