receive_message ( $queue_url, $opt )

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 ReceiveMessage request, it is not returned on subsequent ReceiveMessage 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.

If a message is available in the queue, the call will return immediately. Otherwise, it will wait up to WaitTimeSeconds for a message to arrive. If you do not specify WaitTimeSeconds in the request, the queue attribute ReceiveMessageWaitTimeSeconds is used to determine how long to wait.

You could ask for additional information about each message through the attributes. Attributes that can be requested are [SenderId, ApproximateFirstReceiveTimestamp, ApproximateReceiveCount, SentTimestamp].

Access

public

Parameters

Parameter

Type

Required

Description

$queue_url

string

Required

The URL of the SQS queue to take action on.

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • AttributeName - string|array - Optional - A list of attributes that need to be returned along with each message. The set of valid attributes are [SenderId, ApproximateFirstReceiveTimestamp, ApproximateReceiveCount, SentTimestamp]. Pass a string for a single value, or an indexed array for multiple values.
  • MaxNumberOfMessages - integer - Optional - The maximum number of messages to return. Amazon SQS never returns more messages than this value but may return fewer. All of the messages are not necessarily returned.
  • VisibilityTimeout - integer - Optional - The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request.
  • WaitTimeSeconds - integer - Optional - The duration (in seconds) for which the call will wait for a message to arrive in the queue before returning. If a message is available, the call will return sooner than WaitTimeSeconds.
  • curlopts - array - Optional - A set of values to pass directly into curl_setopt(), where the key is a pre-defined CURLOPT_* constant.
  • returnCurlHandle - boolean - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.

Returns

Type

Description

CFResponse

A CFResponse object containing a parsed HTTP response.

Examples

Receive a message from the queue.

// Receive a message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('example-queue');

// Success?
var_dump($response->isOK());
Result:
bool(true)

Receive a message from the queue and increase the visibility timeout.

// Receive a message, and allow more time to process
$sqs = new AmazonSQS();
$response = $sqs->receive_message('example-queue', array(
	'VisibilityTimeout' => 7200
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Receive a message from the queue, with multiple attributes.

// Receive a single message
$sqs = new AmazonSQS();
$response = $sqs->receive_message('example-queue', array(
	'AttributeName' => array(
		'SenderId',
		'SentTimestamp'
	)
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Related Methods

Source

Method defined in services/sqs.class.php | Toggle source view (16 lines) | View on GitHub

public function receive_message($queue_url, $opt = null)
{
    if (!$opt) $opt = array();
    $opt['QueueUrl'] = $queue_url;
    
    // Optional list (non-map)
    if (isset($opt['AttributeName']))
    {
        $opt = array_merge($opt, CFComplexType::map(array(
            'AttributeName' => (is_array($opt['AttributeName']) ? $opt['AttributeName'] : array($opt['AttributeName']))
        )));
        unset($opt['AttributeName']);
    }

    return $this->authenticate('ReceiveMessage', $opt);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback