Skip to content

Instantly share code, notes, and snippets.

@utdrmac
Created September 22, 2016 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utdrmac/baadb6e3a4d998c61b4709ebb74128ba to your computer and use it in GitHub Desktop.
Save utdrmac/baadb6e3a4d998c61b4709ebb74128ba to your computer and use it in GitHub Desktop.
Test script for AWS SQS
#!/usr/bin/env php
<?php
include 'config.php';
/* This is the EC2 API Client object */
$sqs = Aws\Sqs\SqsClient::factory(array(
'key' => $aws_key,
'secret' => $aws_secret,
'region' => 'us-west-2',
'version'=> 'latest'));
$res = $sqs->getQueueUrl(array(
'QueueName' => 'testqueue',
));
$qUrl = $res->get('QueueUrl');
/* Send message
print "Sending message...\n";
$res = $sqs->sendMessage(array(
'QueueUrl' => $qUrl,
'MessageBody' => "Hello World!",
'DelaySeconds' => 60,
));
printf("MD5: %s | Attribs: %s | ID: %s\n",
$res->get('MD5OfMessageBody'),
$res->get('MD5OfMessageAttributes'),
$res->get('MessageId')
);
sleep(10);
*/
$messages = null;
$promise = $sqs->receiveMessageAsync(array(
'QueueUrl' => $qUrl,
'MaxNumberOfMessages' => 10
));
$result = $promise->wait();
$promise
->then(function ($res) {
//echo 'Got a result: ' . var_dump($res);
$messages = $res->get('Messages');
var_dump($messages);
})
->otherwise(function ($reason) {
echo 'Encountered an error: ' . $reason->getMessage();
});
if ($messages)
{
foreach($messages as $msg)
{
printf("MessageID: %s | Body: %s\n",
$msg['MessageId'],
$msg['Body']
);
$res = $sqs->deleteMessage(array(
'QueueUrl' => $qUrl,
'ReceiptHandle' => $msg['ReceiptHandle']
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment