简体   繁体   中英

AWS SQS error retrieving messages in PHP with empty queue

I have code that is similar to various examples on SQS.

$sqs = \Aws\Sqs\SqsClient::factory(array(
    'key'=>'MYKEY',
    'secret'=>'MYSECRET',
    'region' => 'us-east-1'
));
$queueURL = 'https://sqs.us-east-1.amazonaws.com/MYUSERID/MYQUEUENAME';


$messages = $sqs->receiveMessage(array('QueueUrl' => $queue));
print_r($messages);echo '<BR>';

foreach ($messages->getPath('Messages') as $message) {
    // Do something with the message
    echo $message;
}

However, I am getting an error in the foreach line: Warning: Invalid argument supplied for foreach() . The response to receiveMessage has no Messages, which makes sense because the queue is empty. Here is what print_r($messages) displays:

Guzzle\Service\Resource\Model Object ( [structure:protected] => [data:protected] => Array ( [ResponseMetadata] => Array ( [RequestId] => 96a63acb-685c-5e42-a68b-76b7ea4ff16b ) ) ) 

In the foreach I have also tried $messages['Messages'] and $messages->get('Messages') , all with no luck.

I am running this on a system that is outside of AWS but uses the key/secret for auth.

Thanks!

You can also try:

if ($messages->getPath('Messages')) {
    foreach ($messages->getPath('Messages') as $message) {
        echo "Received: ".$message['Body'];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM