Amazon Simple Queue Service (http://aws.amazon.com/sqs)
2009.08.26
2006-2010 Ryan Parman, Foleeo, Inc., and contributors.
| Simplified BSD License | http://opensource.org/licenses/bsd-license.php |
| CloudFusion | http://getcloudfusion.com |
| Amazon SQS | http://aws.amazon.com/sqs |
Specify the default queue URL.
Specify the queue URL for the U.S.-specific hostname.
Specify the queue URL for the E.U.-specific hostname.
Container for all Amazon SQS-related methods. Inherits additional methods from CloudFusion.
CloudFusion
public function __construct( $key = null, $secret_key = null )
The constructor
public
| key | string (Optional) Your Amazon API Key. If blank, it will look for the AWS_KEY constant. |
| secret_key | string (Optional) Your Amazon API Secret Key. If blank, it will look for the AWS_SECRET_KEY constant. |
boolean false if no valid values are set, otherwise true.
public function set_locale( $locale )
By default SQS will self-select the most appropriate locale. This allows you to explicitly sets the locale for SQS to use.
public
| locale | string (Required) The locale to explicitly set for SQS. Available options are SQS_LOCATION_US and SQS_LOCATION_EU. |
void
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Create a queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->create_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Send a message to the queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->send_message('warpshare-unit-test', 'This is my message.');
// Success
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Delete a queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->delete_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function create_queue( $queue_name, $returnCurlHandle = null )
Creates a new queue to store messages in. You must provide a queue name that is unique within the scope of the queues you own. The queue is assigned a queue URL; you must use this URL when performing actions on the queue. When you create a queue, if a queue with the same name already exists, create_queue() returns the queue URL with an error indicating that the queue already exists.
public
| queue_name | string (Required) The name of the queue to use for this action. The queue name must be unique within the scope of all your queues. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Create a queue
$sqs = new AmazonSQS();
$response = $sqs->create_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Create a queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->create_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function delete_queue( $queue_name, $returnCurlHandle = null )
Deletes the queue specified by the queue URL. This will delete the queue even if it’s not empty.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Delete a queue
$sqs = new AmazonSQS();
$response = $sqs->delete_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Delete a queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->delete_queue('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function list_queues( $queue_name_prefix = null, $returnCurlHandle = null )
Returns a list of your queues. A maximum 1000 queue URLs are returned. If you specify a value for the optional <queue_name_prefix> parameter, only queues with a name beginning with the specified value are returned.
public
| queue_name_prefix | string (Optional) String to use for filtering the list results. Only those queues whose name begins with the specified string are returned. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php // Dependencies require_once 'cloudfusion.class.php'; // List queues $sqs = new AmazonSQS(); $response = $sqs->list_queues(); // Success? var_dump($response->isOK()); /** [Expected output] bool(true) */ ?>
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// List queues with prefix 'a'
$sqs = new AmazonSQS();
$response = $sqs->list_queues('a');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function get_queue_attributes( $queue_name, $attributes = 'All', $returnCurlHandle = null )
Gets one or all attributes of a queue.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| attributes | string | array (Optional) The attribute you want to get. Setting this value to ‘All’ returns all the queue’s attributes. Pass a string for a single attribute, or an indexed array for multiple attributes. Possible values are ‘All’, ‘ApproximateNumberOfMessages’, ‘VisibilityTimeout’, ‘CreatedTimestamp’, ‘LastModifiedTimestamp’, and ‘Policy’. Defaults to ‘All’. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Get queue attributes
$sqs = new AmazonSQS();
$response = $sqs->get_queue_attributes('warpshare-unit-test');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Get queue attributes
$sqs = new AmazonSQS();
$response = $sqs->get_queue_attributes('warpshare-unit-test', 'All');
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Get queue attributes
$sqs = new AmazonSQS();
$response = $sqs->get_queue_attributes('warpshare-unit-test', array(
'ApproximateNumberOfMessages',
'VisibilityTimeout',
'CreatedTimestamp',
'LastModifiedTimestamp',
'Policy'
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function set_queue_attributes( $queue_name, $opt = null )
Sets an attribute of a queue. Currently, you can set only the <VisibilityTimeout> attribute for a queue. See Visibility Timeout for more information.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| opt | array (Required) Associative array of parameters which can have the following keys: |
| VisibilityTimeout | integer (Optional) Must be an integer from 0 to 7200 (2 hours). |
| Policy | string (Optional) A policy generated by <generate_policy()>. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Change visibility timeout
$sqs = new AmazonSQS();
$response = $sqs->set_queue_attributes('warpshare-unit-test', array(
'VisibilityTimeout' => 7200
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function send_message( $queue_name, $message, $returnCurlHandle = null )
Delivers a message to the specified queue.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| message | string (Required) Message size cannot exceed 8 KB. Allowed Unicode characters (according to http://www.w3.org/TR/REC-xml/#charsets): #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Send a message to the queue
$sqs = new AmazonSQS();
$response = $sqs->send_message('warpshare-unit-test', 'This is my message.');
// Success
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Send a message to the queue
$sqs = new AmazonSQS();
$sqs->set_locale(SQS_LOCATION_EU);
$response = $sqs->send_message('warpshare-unit-test', 'This is my message.');
// Success
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function receive_message( $queue_name, $opt = null )
Retrieves one or more messages from the specified queue, including the message body and message ID of each message. Messages returned by this action stay in the queue until you delete them. However, once a message is returned to a receive_message() request, it is not returned on subsequent receive_message() requests for the duration of the <VisibilityTimeout>. If you do not specify a <VisibilityTimeout> in the request, the overall visibility timeout for the queue is used for the returned messages. A default visibility timeout of 30 seconds is set when you create the queue. You can also set the visibility timeout for the queue by using set_queue_attributes(). See Visibility Timeout for more information.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| opt | array (Required) Associative array of parameters which can have the following keys: |
| AttributeName | string | array (Optional) The attribute you want to get. Pass a string for a single attribute, or an indexed array for multiple attributes. Possible values are ‘SenderId’ and ‘SentTimestamp’. |
| VisibilityTimeout | integer (Optional) Must be an integer from 0 to 7200 (2 hours). |
| MaxNumberOfMessages | integer (Optional) Maximum number of messages to return, from 1 to 10. Not necessarily all the messages in the queue are returned. If there are fewer messages in the queue than <MaxNumberOfMessages>, the maximum number of messages returned is the current number of messages in the queue. Defaults to 1 message. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Receive a message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('warpshare-unit-test');
// Store the message receipt in a temp file so that the delete message can grab it later.
// This is The Wrong Way To Do It™
file_put_contents('receipt_handle.cache', (string) $response->body->ReceiveMessageResult->Message->ReceiptHandle); // Pass data to delete_message.
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Receive a message, and allow more time to process
$sqs = new AmazonSQS();
$response = $sqs->receive_message('warpshare-unit-test', array(
'VisibilityTimeout' => 7200
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Receive a single message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('warpshare-unit-test', array(
'MaxNumberOfMessages' => 1
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Receive a single message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('warpshare-unit-test', array(
'AttributeName' => 'SenderId'
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Receive a single message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('warpshare-unit-test', array(
'AttributeName' => array(
'SenderId',
'SentTimestamp'
)
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function delete_message( $queue_name, $receipt_handle, $returnCurlHandle = null )
Unconditionally removes the specified message from the specified queue. Even if the message is locked by another reader due to the visibility timeout setting, it is still deleted from the queue.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| receipt_handle | string (Required) The receipt handle of the message to delete, returned by receive_message(). |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Delete a message we've read
$sqs = new AmazonSQS();
$response = $sqs->delete_message('warpshare-unit-test',
file_get_contents('receipt_handle.cache') // See receive_message() example for why we're doing this.
);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function change_message_visibility( $receipt_handle, $visibility_timeout, $returnCurlHandle = null )
Changes the visibility timeout of a specified message in a queue to a new value. The maximum allowed timeout value you can set the value to is 12 hours. This means you can’t extend the timeout of a message in an existing queue to more than a total visibility timeout of 12 hours.
For example, let’s say you have a message and its default message visibility timeout is 30 minutes. You could call ChangeMessageVisiblity with a value of two hours and the effective timeout would be two hours and 30 minutes. When that time comes near you could again extend the time out by calling ChangeMessageVisiblity, but this time the maximum allowed timeout would be 9 hours and 30 minutes.
public
| receipt_handle | string (Required) The receipt handle associated with the message whose visibility timeout you want to change. This parameter is returned by receive_message(). |
| visibility_timeout | string (Required) The new value for the message’s visibility timeout (in seconds). This value is limited to 43200 seconds (12 hours). |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
public function add_permission( $queue_name, $label, $permissions, $returnCurlHandle = null )
Adds a permission to a queue for a specific principal. This allows for sharing access to the queue. When you create a queue, you have full control access rights for the queue. Only you (as owner of the queue) can grant or deny permissions to the queue.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
| label | string (Required) The unique identification of the permission you’re setting. Maximum 80 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed. |
| permissions | array (Required) An associative array of AWS account numbers (key) and the actions they’re allowed to execute (value). AWS account numbers are for those who will be given permission. Actions can be passed as a string for a single action, or an indexed array for multiple actions. Valid values are ‘*’, ‘SendMessage’, ‘ReceiveMessage’, ‘DeleteMessage’, ‘ChangeMessageVisibility’, or ‘GetQueueAttributes’. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Add permissions to a queue
$sqs = new AmazonSQS();
$response = $sqs->add_permission('warpshare-unit-test', 'WarpShareTesting', array(
'133904017518' => array(
'GetQueueAttributes',
'ChangeVisbilityTimeout'
)
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>| AWS Method | http://docs.amazonwebservices.com |
| Related | add_permission(), remove_permission(), <generate_policy()> |
public function remove_permission( $label, $returnCurlHandle = null )
Revokes any permissions in the queue policy that matches the Label parameter. Only the owner of the queue can remove permissions.
public
| label | string (Required) This should match the label you set in add_permission(). |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
| AWS Method | http://docs.amazonwebservices.com |
| Related | add_permission(), remove_permission(), <generate_policy()> |
public function get_queue_size( $queue_name )
Retrieves the approximate number of messages in the queue.
public
| queue_name | string (Required) The name of the queue to perform the action on. |
integer The Approximate number of messages in the queue.
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Get queue size
$sqs = new AmazonSQS();
$response = $sqs->get_queue_size('warpshare-unit-test');
// Success?
var_dump($response);
/** [Expected output]
*/
?>| Related | get_queue_attributes() |