@denis
To get data from AWS SQS (Simple Queue Service) in Laravel, you can use the AWS SDK for PHP. Here's a step-by-step guide on how to do this:
1
|
composer require aws/aws-sdk-php |
1 2 3 4 5 6 7 8 9 10 |
use AwsSqsSqsClient; $sqsClient = new SqsClient([ 'version' => 'latest', 'region' => 'us-west-2', 'credentials' => [ 'key' => 'YOUR_AWS_ACCESS_KEY', 'secret' => 'YOUR_AWS_SECRET_KEY', ], ]); |
Replace 'YOUR_AWS_ACCESS_KEY'
and 'YOUR_AWS_SECRET_KEY'
with your AWS access key and secret key.
1 2 3 4 5 6 |
$result = $sqsClient->receiveMessage([ 'QueueUrl' => 'YOUR_QUEUE_URL', 'MaxNumberOfMessages' => 10, // Number of messages to receive ]); $messages = $result->get('Messages'); |
Replace 'YOUR_QUEUE_URL'
with the URL of your SQS queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
foreach ($messages as $message) { $body = json_decode($message['Body'], true); // Process the message body // For example: // echo $body['message']; // Delete the message from the queue after processing $sqsClient->deleteMessage([ 'QueueUrl' => 'YOUR_QUEUE_URL', 'ReceiptHandle' => $message['ReceiptHandle'], ]); } |
Remember to handle exceptions and error checking in your code to ensure robustness and reliability when interacting with AWS services.
That's it! You can now receive and process messages from an AWS SQS queue in your Laravel application.