AWS SQS Deleting Messages Using the Receive Handle

I am trying to set up SQS and after receiving a message I need to remove it from the queue.

Client creation -

$client = Aws\Sqs\SqsClient::factory(array(
                    'key'    => '******',
                    'secret' => '******',
                    'region' => 'ap-southeast-1'
            ));

      

Sending a message

public static function SendMessage()
    {
        if(!isset(self::$queueUrl))
            self::getQueueUrl();

        $command = "This is a command";
        $commandstring = json_encode($command);

        self::$client->sendMessage(array(
                'QueueUrl'    => self::$queueUrl,
                'MessageBody' => $commandstring,
        ));
    }

      

Receiving a message

public static function RecieveMessage()
    {
        if(!isset(self::$queueUrl))
            self::getQueueUrl();

        $result = self::$client->receiveMessage(array(
                'QueueUrl' => self::$queueUrl,
        ));

//      echo "Message Recieved >>  ";
        print_r($result);
        foreach ($result->getPath('Messages/*/Body') as $messageBody) {
            // Do something with the message
            echo $messageBody;
            //print_r(json_decode($messageBody));
        }

        foreach ($result->getPath('Messages/*/ReceiptHandle') as $ReceiptHandle) {
            self::$client->deleteMessage(self::$queueUrl, $ReceiptHandle);
        }

    }

      

When I try to delete a message using the Handle Handle in the receive message code, I get an error from Guzzle - Allowable fatal error: argument 2 passed to Guzzle \ Service \ Client :: getCommand () must be an array given by string.

Now, after searching a lot, I was able to find similar questions that claim they were using the wrong SDK version. I still can't narrow it down. I am using the zip version of the latest sdk 2.6.15

+3


source to share


1 answer


Why don't you try this:

self::$client->deleteMessage(array(
    'QueueUrl' => self::$queueUrl,
    'ReceiptHandle' => $ReceiptHandle,
));

      



An example of basic formatting in API docs forSqsClient::deleteMessage()

(and other operations) should help. All methods that perform operations take exactly one parameter, which is an associative array of operation parameters. You should read the SDK Getting Started Guide (if you haven't already) which talks about how to perform operations in general.

+4


source







All Articles