Skip to content

Instantly share code, notes, and snippets.

@djordn
Last active September 17, 2018 13:33
Show Gist options
  • Save djordn/ebb5c37c02ee1d620797890241dc56b8 to your computer and use it in GitHub Desktop.
Save djordn/ebb5c37c02ee1d620797890241dc56b8 to your computer and use it in GitHub Desktop.
AWS SNS and SQS sample
{
"require": {
"aws/aws-sdk-php": "2.*"
}
}
<?php
error_reporting( E_ALL );
ini_set('display_errors', true );
/*
* This file is part of the php-gelf package.
*
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__ . '/vendor/autoload.php';
// When creating a logger without any options, it logs automatically to localhost:12201 via UDP
// For a move advanced configuration, check out the advanced.php example
use Aws\Sns\SnsClient;
function getSimpleCredentials()
{
return array(
'credentials' => array(
'key' => 'put_your_key_here',
'secret' => 'put_your_secret_here',
),
//SP
'region' => 'sa-east-1'
);
}
$client = SnsClient::factory( getSimpleCredentials()) ;
echo '<pre />';
$obj = new stdClass();
//Just a simple test message
$obj->type_event = "PROVA_RG146_EV182";
$obj->aluno = [ "name" => "Tiago Mission", "ra" => "XPTO4202", "unit_id" => "OLIM-XXX", "shortname" => "XXX84", "class_id" => "6XXXXX151A1" ];
$obj->content = [ "_id" => "TestSQS1", "absent" => false, "value" => "500" ];
$obj->origin = "url_origin_evento";
$obj->sent_date = "2018-01-28T11:54:22+00:00";
$result = $client->publish(array(
'TopicArn' => 'put_your_arn_here',
// Message is required
'Message' => json_encode( $obj ),
'Subject' => 'Sent by PHP ' . date('H:i:s dd/mm/Y'),
'MessageId' => date('Ymhis'),
// 'MessageStructure' => 'string',
// 'MessageAttributes' => array(
// // Associative array of custom 'String' key names
// 'String' => array(
// // DataType is required
// 'DataType' => 'json',
// // 'StringValue' => 'string',
// // 'BinaryValue' => 'string',
// ),
// ... repeated
// ),
));
die(var_dump( $result ) );
<?php
error_reporting( E_ALL );
ini_set('display_errors', true );
/*
* This file is part of the php-gelf package.
*
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__ . '/vendor/autoload.php';
// When creating a logger without any options, it logs automatically to localhost:12201 via UDP
// For a move advanced configuration, check out the advanced.php example
use Aws\Sqs\SqsClient;
$queueUrl = 'https://queue-url';
function getSimpleCredentials()
{
return array(
'credentials' => array(
'key' => 'put_your_key_here',
'secret' => 'put_your_secret_here',
),
'region' => 'sa-east-1'
);
}
$client = SqsClient::factory( getSimpleCredentials()) ;
// Get the client from the builder by namespace
// $client = $aws->get('Sqs');
echo '<pre />';
// die(var_dump( $client->createQueue(array('QueueName' => 'my-queue')) ));
$result = $client->receiveMessage(array(
'QueueUrl' => $queueUrl,
'WaitTimeSeconds' => 2,
));
foreach ($result->getPath('Messages/*/Body') as $messageBody) {
// Do something with the message
echo $messageBody;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment