Follow Us

Recent Blog Posts

Amazon Simple Queue Service

This guide focuses on the AWS SDK for PHP client for Amazon Simple Queue Service. This guide assumes that you have already downloaded and installed the AWS SDK for PHP. See Installation for more information on getting started.

Creating a client

First you need to create a client object using one of the following techniques.

Factory method

The easiest way to get up and running quickly is to use the Aws\Sqs\SqsClient::factory() method and provide your credential profile (via the profile option), which identifies the set of credentials you want to use from your ~/.aws/credentials file (see Using the AWS credentials file and credential profiles).

A region parameter is required. You can find a list of available regions using the Regions and Endpoints reference.

use Aws\Sqs\SqsClient;

$client = SqsClient::factory(array(
    'profile' => '<profile in your aws credentials file>',
    'region'  => '<region name>'

You can provide your credential profile like in the preceding example, specify your access keys directly (via key and secret), or you can choose to omit any credential information if you are using AWS Identity and Access Management (IAM) roles for EC2 instances or credentials sourced from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.


The profile option and AWS credential file support is only available for version 2.6.1 of the SDK and higher. We recommend that all users update their copies of the SDK to take advantage of this feature, which is a safer way to specify credentials than explicitly providing key and secret.

Service builder

A more robust way to connect to Amazon Simple Queue Service is through the service builder. This allows you to specify credentials and other configuration settings in a configuration file. These settings can then be shared across all clients so that you only have to specify your settings once.

use Aws\Common\Aws;

// Create a service builder using a configuration file
$aws = Aws::factory('/path/to/my_config.json');

// Get the client from the builder by namespace
$client = $aws->get('Sqs');

For more information about configuration files, see Configuring the SDK.

Creating a queue

Now, let's create a queue. You can create a standard queue by just providing a name. Make sure to get the queue's URL from the result, since the queue URL is the unique identifier used to specify the queue in order to send and receive messages.

$result = $client->createQueue(array('QueueName' => 'my-queue'));
$queueUrl = $result->get('QueueUrl');

You can also set attributes on your queue when you create it.

$result = $client->createQueue(array(
    'QueueName'  => 'my-queue',
    'Attributes' => array(
        'DelaySeconds'       => 5,
        'MaximumMessageSize' => 4096, // 4 KB
$queueUrl = $result->get('QueueUrl');

Or you can also set queue attributes later.

$result = $client->setQueueAttributes(array(
    'QueueUrl'   => $queueUrl,
    'Attributes' => array(
        'VisibilityTimeout' => 2 * 60 * 60, // 2 min

Sending messages

Sending a message to a queue is straight forward with the SendMessage command.

    'QueueUrl'    => $queueUrl,
    'MessageBody' => 'An awesome message!',

You can overwrite the queue's default delay for a message when you send it.

    '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.

$result = $client->receiveMessage(array(
    'QueueUrl'        => $queueUrl,
    'WaitTimeSeconds' => 10,


You can also configure long-polling at the queue level by setting the ReceiveMessageWaitTimeSeconds queue attribute.