Amazon SQS

Amazon Simple Queue Service (http://aws.amazon.com/sqs)

Version

2009.08.26

Copyright

2006-2010 Ryan Parman, Foleeo, Inc., and contributors.

License

Simplified BSD Licensehttp://opensource.org/licenses/bsd-license.php

See Also

CloudFusionhttp://getcloudfusion.com
Amazon SQShttp://aws.amazon.com/sqs

Constants

SQS_DEFAULT_URL

Specify the default queue URL.

SQS_LOCATION_US

Specify the queue URL for the U.S.-specific hostname.

SQS_LOCATION_EU

Specify the queue URL for the E.U.-specific hostname.

AmazonSQS

Container for all Amazon SQS-related methods.  Inherits additional methods from CloudFusion.

Extends

CloudFusion

Functions

__construct()

public function __construct($key =  null,
$secret_key =  null)

The constructor

Access

public

Parameters

keystring (Optional) Your Amazon API Key.  If blank, it will look for the AWS_KEY constant.
secret_keystring (Optional) Your Amazon API Secret Key.  If blank, it will look for the AWS_SECRET_KEY constant.

Returns

boolean false if no valid values are set, otherwise true.

set_locale()

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.

Access

public

Parameters

localestring (Required) The locale to explicitly set for SQS.  Available options are SQS_LOCATION_US and SQS_LOCATION_EU.

Returns

void

Examples

AmazonSQS::create_queue (EU)
<?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)
*/
?>
AmazonSQS::send_message (EU)
<?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)
*/
?>
AmazonSQS::delete_queue (EU)
<?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)
*/
?>

create_queue()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to use for this action.  The queue name must be unique within the scope of all your queues.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::create_queue
<?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)
*/
?>
AmazonSQS::create_queue (EU)
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryCreateQueue.html
Relateddelete_queue(), list_queues(), get_queue_attributes(), set_queue_attributes()

delete_queue()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::delete_queue
<?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)
*/
?>
AmazonSQS::delete_queue (EU)
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryDeleteQueue.html
Relatedcreate_queue(), list_queues(), get_queue_attributes(), set_queue_attributes()

list_queues()

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.

Access

public

Parameters

queue_name_prefixstring (Optional) String to use for filtering the list results.  Only those queues whose name begins with the specified string are returned.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::list_queues
<?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)
*/
?>
AmazonSQS::list_queues, with prefix
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryListQueues.html
Relatedcreate_queue(), delete_queue(), get_queue_attributes(), set_queue_attributes()

get_queue_attributes()

public function get_queue_attributes($queue_name,  
$attributes =  'All',
$returnCurlHandle =  null)

Gets one or all attributes of a queue.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
attributesstring | 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’.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::get_queue_attributes
<?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)
*/
?>
AmazonSQS::get_queue_attributes, passing a single string as an attribute.
<?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)
*/
?>
AmazonSQS::get_queue_attributes, passing an array of attributes.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryGetQueueAttributes.html
Relatedcreate_queue(), delete_queue(), list_queues(), set_queue_attributes()

set_queue_attributes()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

VisibilityTimeoutinteger (Optional) Must be an integer from 0 to 7200 (2 hours).
Policystring (Optional) A policy generated by <generate_policy()>.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::set_queue_attributes, changing visibility timeout
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryGetQueueAttributes.html
Relatedcreate_queue(), delete_queue(), list_queues(), get_queue_attributes()

send_message()

public function send_message($queue_name,  
$message,  
$returnCurlHandle =  null)

Delivers a message to the specified queue.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
messagestring (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].
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::send_message
<?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)
*/
?>
AmazonSQS::send_message (EU)
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QuerySendMessage.html
Relatedsend_message(), receive_message(), delete_message(), change_message_visibility()

receive_message()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

AttributeNamestring | 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’.
VisibilityTimeoutinteger (Optional) Must be an integer from 0 to 7200 (2 hours).
MaxNumberOfMessagesinteger (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.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::receive_message
<?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)
*/
?>
AmazonSQS::receive_message, change visibility timeout
<?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)
*/
?>
AmazonSQS::receive_message, only a single message
<?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)
*/
?>
AmazonSQS::receive_message, single attribute
<?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)
*/
?>
AmazonSQS::receive_message, multiple attributes
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryReceiveMessage.html
Relatedsend_message(), receive_message(), delete_message(), change_message_visibility()

delete_message()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
receipt_handlestring (Required) The receipt handle of the message to delete, returned by receive_message().
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::delete_message
<?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)

*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryDeleteMessage.html
Relatedsend_message(), receive_message(), delete_message(), change_message_visibility()

change_message_visibility()

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.

Access

public

Parameters

receipt_handlestring (Required) The receipt handle associated with the message whose visibility timeout you want to change.  This parameter is returned by receive_message().
visibility_timeoutstring (Required) The new value for the message’s visibility timeout (in seconds).  This value is limited to 43200 seconds (12 hours).
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/index.html?Query_QueryChangeMessageVisibility.html
Relatedsend_message(), receive_message(), delete_message(), change_message_visibility()

add_permission()

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.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.
labelstring (Required) The unique identification of the permission you’re setting.  Maximum 80 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed.
permissionsarray (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’.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

AmazonSQS::add_permission
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/index.html?Query_QueryAddPermission.html
Relatedadd_permission(), remove_permission(), <generate_policy()>

remove_permission()

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.

Access

public

Parameters

labelstring (Required) This should match the label you set in add_permission().
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

See Also

AWS Methodhttp://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/index.html?Query_QueryRemovePermission.html
Relatedadd_permission(), remove_permission(), <generate_policy()>

get_queue_size()

public function get_queue_size($queue_name)

Retrieves the approximate number of messages in the queue.

Access

public

Parameters

queue_namestring (Required) The name of the queue to perform the action on.

Returns

integer The Approximate number of messages in the queue.

Examples

AmazonSQS::get_queue_size
<?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] 
*/
?>

See Also

Relatedget_queue_attributes()