SQS library for PHP (Simple-AWS)

Sample Code & Libraries>Amazon SQS>SQS library for PHP (Simple AWS)
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

PHP 5 library for use with Amazon Simple Queue Service. The classes generally try to follow PEAR-ish conventions for package names. No "require_once" statements have been used for including dependent classes. You will need to use an _autoload function instead.

Details

Submitted By: mikebrittain
AWS Products Used: Amazon SQS
Language(s): PHP
License: New BSD
Source Control Access: http://code.google.com/p/simple-aws/source/checkout
Created On: September 18, 2008 1:40 PM GMT
Last Updated: September 21, 2008 7:41 PM GMT
Download

Prerequisites

  • PHP 5 with cURL support

Project Site

Up to date information about this package can be found at the simple-aws project site.

Documentation

Authentication

AWS requires the use of an access key and a secret key to maintain security on your own AWS account. The SQS package makes use of an .ini file for storing these credentials.

Currently, this .ini file is expected to be located at "/etc/amazon/aws.ini" on your server. If you want to change this location, it can be edited in the Services_Amazon_Account class:

private $_aws_ini_file = '/etc/amazon/aws.ini';

Managing Queues

The Services_Amazon?_SQS_QueueManager? class can be used for listing, creating, deleting, or managing properties on your queues:

$manager = new Services_Amazon_SQS_QueueManager();

// Get a list of all queues in the account.
$list = $manager->listQueues();

// Get a list of queues that start with the prefix "db-jobs-".
$list = $manager->listQueues("db-jobs-");

// Add a new queue for managing changes to usernames (arbitrary example).
$success = $manager->createQueue("db-jobs-username-changes");

// Remove an obsolete queue. Queues are referenced by full URLs.
$success = $manager->deleteQueue("http://queue.amazonaws.com/db-jobs-moderate-images");

Command-line Manager

Additionally, there is a PHP script in the /bin directory that can be used for managing queues. The bootstrap.php file in /bin is used for setting up the proper PHP include path and defining an autoload function. It should be customized for your own environment.

Usage:

$ ./sqs
A command-line interface to Amazon's Simple Queue Service.

Usage:
  ./sqs [options]

Options:
  --list        List all available queues in account.
  --add=NAME    Add a new queue.
  --delete=URL  Delete a queue.
  -h, --help    show this help message and exit
  --version     show the program version and exit

Examples:

$ ./sqs --list
QUEUE                                          ITEMS (APPROX.)   VIS. TIMEOUT
http://queue.amazonaws.com/queue-one           257               30          
http://queue.amazonaws.com/queue-two           108               30          

$ ./sqs --add=queue-three
New queue has been added.  It may take up to 60 seconds for it to appear in your list of queues.

$ ./sqs --list
QUEUE                                          ITEMS (APPROX.)   VIS. TIMEOUT
http://queue.amazonaws.com/queue-one           257               30          
http://queue.amazonaws.com/queue-two           108               30          
http://queue.amazonaws.com/queue-three         0                 30 

Using Queues

Add new jobs to a queue, typically as serialized data.

// Get an object for a DB job queue.
$queue = new Services_Amazon_SQS_Queue("http://queue.amazonaws.com/db-jobs-username-changes");

// Create a job to be added to the queue.
$job = serialize( array("old_username" => "spanks543", "new_username" => "spanky") );
$result = $queue->push($job);

Job processor script will pick up an item from the queue, complete the job, then delete the item from the queue.

$queue = new Services_Amazon_SQS_Queue("http://queue.amazonaws.com/db-jobs-username-changes");

// Grab a task from the queue (makes it invisible to other job processors for a time limit).
$messages = $queue->pop();
$job = unserialize($messages[0]['message']);

// ... Process the job ...

// Delete the completed job from the queue.
$queue->delete($messages[0]['handle']);
©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.