To learn about Amazon SQS messages, see Sending a Message to an Amazon SQS Queue and Receiving and Deleting a Message from an Amazon SQS Queue in the Amazon Simple Queue Service Developer Guide.
The examples below show how to:
All the example code for the AWS SDK for PHP is available here on GitHub.
Before running the example code, configure your AWS credentials, as described in Credentials.
require 'vendor/autoload.php';
use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
$client = new SqsClient([
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2012-11-05'
]);
$params = [
'DelaySeconds' => 10,
'MessageAttributes' => [
"Title" => [
'DataType' => "String",
'StringValue' => "The Hitchhiker's Guide to the Galaxy"
],
"Author" => [
'DataType' => "String",
'StringValue' => "Douglas Adams."
],
"WeeksOn" => [
'DataType' => "Number",
'StringValue' => "6"
]
],
'MessageBody' => "Information about current NY Times fiction bestseller for week of 12/11/2016.",
'QueueUrl' => 'QUEUE_URL'
];
try {
$result = $client->sendMessage($params);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
require 'vendor/autoload.php';
use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
$queueUrl = "QUEUE_URL";
$client = new SqsClient([
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2012-11-05'
]);
try {
$result = $client->receiveMessage(array(
'AttributeNames' => ['SentTimestamp'],
'MaxNumberOfMessages' => 1,
'MessageAttributeNames' => ['All'],
'QueueUrl' => $queueUrl, // REQUIRED
'WaitTimeSeconds' => 0,
));
if (count($result->get('Messages')) > 0) {
var_dump($result->get('Messages')[0]);
$result = $client->deleteMessage([
'QueueUrl' => $queueUrl, // REQUIRED
'ReceiptHandle' => $result->get('Messages')[0]['ReceiptHandle'] // REQUIRED
]);
} else {
echo "No messages in queue. \n";
}
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}