AWS SDK for PHP
Developer Guide

Using Queues in Amazon SQS with AWS SDK for PHP version 3

To learn about Amazon SQS queues, see How SQS Queues Work.

The following examples show how to:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

Return a List of Queues

Imports

require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException;

Sample Code

$client = new SqsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2012-11-05' ]); try { $result = $client->listQueues(); foreach ($result->get('QueueUrls') as $queueUrl) { echo "$queueUrl\n"; } } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Create a Queue

Imports

require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException;

Sample Code

$queueName = "SQS_QUEUE_NAME"; $client = new SqsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2012-11-05' ]); try { $result = $client->createQueue(array( 'QueueName' => $queueName, 'Attributes' => array( 'DelaySeconds' => 5, 'MaximumMessageSize' => 4096, // 4 KB ), )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Return the URL of a Queue

Imports

require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException;

Sample Code

$queueName = "SQS_QUEUE_NAME"; $client = new SqsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2012-11-05' ]); try { $result = $client->getQueueUrl([ 'QueueName' => $queueName // REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Delete a Queue

Imports

require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException;

Sample Code

$queueUrl = "SQS_QUEUE_URL"; $client = new SqsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2012-11-05' ]); try { $result = $client->deleteQueue([ 'QueueUrl' => $queueUrl // REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }