Installation guide
use Aws\Common\Enum\Size;
use Aws\Sqs\Enum\QueueAttribute;
$result = $client->createQueue(array(
'QueueName' => 'my-queue',
'Attributes' => array(
QueueAttribute::DELAY_SECONDS => 5,
QueueAttribute::MAXIMUM_MESSAGE_SIZE => 4 * Size::KB,
),
));
$queueUrl = $result->get('QueueUrl');
Or you can also set queue attributes later.
use Aws\Common\Enum\Time;
use Aws\Sqs\Enum\QueueAttribute;
$result = $client->setQueueAttributes(array(
'QueueUrl' => $queueUrl,
'Attributes' => array(
QueueAttribute::VISIBILITY_TIMEOUT => 2 * Time::MINUTES,
),
));
Sending messages
Sending a message to a queue is straight forward with the SendMessage command.
$client->sendMessage(array(
'QueueUrl' => $queueUrl,
'MessageBody' => 'An awesome message!',
));
You can overwrite the queue's default delay for a message when you send it.
$client->sendMessage(array(
'QueueUrl' => $queueUrl,
'MessageBody' => 'An awesome message!',
'DelaySeconds' => 30,
));
Receiving messages
Receiving messages is done with the ReceiveMessage command.
$result = $client->receiveMessage(array(
'QueueUrl' => $queueUrl,
));
foreach ($result->getPath('Messages/*/Body') as $messageBody) {
// Do something with the message
echo $messageBody;
}
By default, only one message will be returned. If you want to get more messages, make sure to use the
MaxNumberOfMessages parameter and specify a number of messages (1 to 10). Remember that you are not
guaranteed to receive that many messages, but you can receive up to that amount depending on how many are
actually in the queue at the time of your request.
SQS also supports "long polling", meaning that you can instruct SQS to hold the connection open with the SDK for
up to 20 seconds in order to wait for a message to arrive in the queue. To configure this behavior, you must use the
WaitTimeSeconds parameter.
Amazon Simple Queue Service
119