-
-
Save YugalXD/f7e4e43b1ff9a9fd33a6a25ea9856102 to your computer and use it in GitHub Desktop.
Sending and receiving messages from/to AWS SQS via PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// curl -sS https://getcomposer.org/installer | php | |
// php composer.phar require aws/aws-sdk-php | |
// export AWS_ACCESS_KEY_ID=... | |
// export AWS_SECRET_ACCESS_KEY=... | |
$queueUrl = '<INSERT_URL_OF_EXISTING_QUEUE_HERE>'; | |
require_once 'vendor/autoload.php'; | |
$sdk = new \Aws\Sdk(); | |
$sqsClient = $sdk->createSqs(['region' => 'eu-west-1', 'version' => '2012-11-05']); | |
echo "Sending message\n"; | |
$sqsClient->sendMessage(array( | |
'QueueUrl' => $queueUrl, | |
'MessageBody' => 'Hello World!', | |
)); | |
echo "Receiving messages\n"; | |
$result = $sqsClient->receiveMessage([ | |
'AttributeNames' => ['All'], | |
'MaxNumberOfMessages' => 10, | |
'QueueUrl' => $queueUrl, | |
]); | |
foreach ($result->search('Messages[]') as $message) { | |
echo "- Message: {$message['Body']} (Id: {$message['MessageId']})\n"; | |
} | |
echo "Deleting messages\n"; | |
foreach ($result->search('Messages[]') as $message) { | |
$sqsClient->deleteMessage([ | |
'QueueUrl' => $queueUrl, | |
'ReceiptHandle' => $message['ReceiptHandle'] | |
]); | |
echo "- Deleted: {$message['MessageId']})\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment