I am using the aws-sdk-php-2 API from Amazom to receive messages from Amazon Simple Queue Service (Amazon SQS).

I am following the given Tutorial: http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/service-sqs.html

My problem is: If I use the code given, I receive the Message Body as a String (containing XML). Is there a way to fetch the message body as an object, or altenatively as an associative array?

I realize that there are ways to transform XML to an PHP object (SimpleXML comes to mind), but it seems to be such an obvious feature of the api that I cannot imagine that it does not offer it. But I cannot find it in the API (http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Sqs.SqsClient.html)

My current code:

<?php

use Aws\Sqs\SqsClient;

$client = SqsClient::factory(array(
    'key'    => '...',
    'secret' => '...',
    'region' => 'eu-west-1'
));

$queueUrl = 'https://sqs.eu-west-1.amazonaws.com/...';

$result = $client->receiveMessage(array(
    'QueueUrl' => $queueUrl,
    'MaxNumberOfMessages' => 3
));

foreach ($result->getPath('Messages/*/Body') as $messageBody) {
    echo "<pre>";
    echo htmlentities( print_r( $messageBody, true ) );
    echo "</pre>";
}

// or alternatively

foreach( $result->get('Messages') as $message ){

    echo "<pre>";
    echo htmlentities( print_r( $message['Body'], true ) );
    echo "</pre>";

    echo "<hr />";
}

?>
有帮助吗?

解决方案

The SQS system doesn't really care what is in the body as this is something that is defined by your application.

If you want to pass objects through SQS, you will need to serialize them in some format. Any of the following could work: XML, JSON, PHP serialization (You may not want to use this option if you need to send or receive messages in systems that do not use php)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top