PHP code example of tedicela / sqs-simple

1. Go to this page and download the library: Download tedicela/sqs-simple library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tedicela / sqs-simple example snippets




sSimple\SqsMessenger;

$AwsConfig = [
    'AWS_KEY'=>'', //You should put your AWS_KEY 
    'AWS_SECRET_KEY'=>'', //You should put your AWS_SECRET_KEY 
    'AWS_REGION'=>'eu-west-1', //You should put your AWS_REGION 
    'API_VERSION'=>'2012-11-05'
];
$messenger = new SqsMessenger($AwsConfig);

/* if a publish message request fails then it will retry again */
// $messenger->RetryTimesOnFail = 2;
/* seconds to wait after failed request to retry again */
// $messenger->WaitBeforeRetry = 1; //seconds

$queue = "<Your queueUrl>";
$message = "This is a message for SQS";
$messageAttributes = [];
$delaySeconds = 10; // (Not FIFO Queue type) - The time in seconds that the delivery of all messages in the queue will be delayed. An integer from 0 to 900 (15 minutes). The default for this attribute is 0 (zero).
$messageGroupId = ''; // (FIFO Queue type) - The tag that specifies that a message belongs to a specific message group. Messages that belong to the same message group are always processed one by one, in a strict order relative to the message group (however, messages that belong to different message groups might be processed out of order).
$messageDeduplicationId = ''; // (FIFO Queue type) - The token used for deduplication of sent messages. If a message with a particular message deduplication ID is sent successfully, any messages sent with the same message deduplication ID are accepted successfully but aren't delivered during the 5-minute deduplication interval. The queue should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly.
$messenger->publish( $queue, $message, $messageAttributes, $delaySeconds, $messageGroupId, $messageDeduplicationId);



sSimple\SqsWorker;

/*
  You can pass the AWS configuration into constructor
  but this is optional. If you don't pass this configuration 
  you should set an authenticated SqsClient Object 
  like in the comment below
*/
$AwsConfig = [
    'AWS_KEY'=>'', //You should put your AWS_KEY 
    'AWS_SECRET_KEY'=>'', //You should put your AWS_SECRET_KEY 
    'AWS_REGION'=>'eu-west-1', //You should put your AWS_REGION 
    'API_VERSION'=>'2012-11-05',
];
$worker = new SqsWorker($AwsConfig);

/*
  You can set an already authenticated SqsClient Object
  if set this you don't need to pass the AwsConfig above
*/
// $worker->SqsClient = $ExistingSqsClient;
/* seconds to wait before checking again if no messages was found */
// $worker->Sleep = 10;
/* how many seconds with wait for messages in SQS to be available in one check */
// $worker->WaitTimeSeconds = 20;
/* how many messages to get from queue when the worker checks */
// $worker->MaxNumberOfMessages = 1;
/*    
  After the worker get a message and starts elaborating it 
  how many seconds the message should not be available to other workers
*/
// $worker->VisibilityTimeout = 3600;

$queueUrl = "<Your queueUrl>";
$worker->listen($queueUrl, function($message){
    echo "\n this is the working process\n";
    
    var_dump($message);
    
    /*
      You should return TRUE when you want to remove the message from queue (ACK) 
      or return FALSE when your job failed and you want to return the message back to queue(NACK) 
      to retry later or by another worker
    */
    return true;
    
},
function($exceptionMessage, $errorCounter){ // $errorHandlerCallback - this is optional as parameter
	/*
		$exceptionMessage is the $e->getMessage()
		$errorCounter - how many times in a row errors occured, if it's 5 then listening will stop
	*/
});