Using queues in Amazon SQS with AWS SDK for PHP Version 3 - AWS SDK for PHP

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 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS SDK for PHP, as described in Basic usage.

Return a list of queues

Imports

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

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\Exception\AwsException; use Aws\Sqs\SqsClient;

Sample Code

$queueName = "SQS_QUEUE_NAME"; $client = new SqsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2012-11-05' ]); try { $result = $client->createQueue([ 'QueueName' => $queueName, 'Attributes' => [ '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\Exception\AwsException; use Aws\Sqs\SqsClient;

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\Exception\AwsException; use Aws\Sqs\SqsClient;

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()); }