Using queues in Amazon SQS with Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using queues in Amazon SQS with Amazon 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 Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon 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()); }